Before you go, check out these stories!

0
Hackernoon logoAigle vs Bluebird by@suguru.motegi

Aigle vs Bluebird

Author profile picture

@suguru.motegiSuguru Motegi

bench doxbee
bench parallel

Aigle is an ideal promise library that I created to improve inefficiencies that I noticed when using other promise libraries like Bluebird. On top of being an impressive benchmark exercise, it is a production-ready library that implements the Promise A+ standard.

Benchmark

The above benchmark exercises indicates that Aigle is faster than Bluebird. This was achieved by avoiding unnecessary memory allocation and function calls. For instance, Aigle is able to optimize itโ€™s dependencies while Bluebirdโ€™s performance is affected if itโ€™s dependencies use different versions of it. For more information on how Aigle is faster than Bluebird, I would encourage you to read my article on it.

Functions

Aigle makes your code less verbose and contains many functions similar to Lodash and Async. As a result, you would be able to code faster and have a much easier time maintaining code.

Aigle.map vs Bluebird.map

Compared to Bluebird.map, Aigle.map is 1.5x faster and also supports an object as a first argument unlike Bluebird.map.

const object = {
a: 1,
b: 2,
c: 3
};
Blubird.map(Object.keys(object), key => {
const value = object[key];
...
});
Aigle.map(object, (value, key) => { ... });

If you want to use concurrency, Aigle has Aigle.mapLimit which is similar to Async.mapLimit.

const array = [1, 2, 3, 4, 5];
Bluebird.map(array, value => {
console.log(value); // 1, 2, 5, 4, 3 the order is not ensured
return Bluebird.delay(10);
}, { concurrency: 2 });
Aigle.mapLimit(array, 2, value => {
console.log(value); // 1, 2, 3, 4, 5 the order is ensured
return Aigle.delay(10);
});
// you could use default limit, the limit is 8
Aigle.mapLimit(array, value => {
console.log(value); // 1, 2, 3, 4, 5
return Aigle.delay(10);
});

In addition, Aigle supports shorthand notation similar to Lodash for cleaner code.

const array = [{ name: 'barney' }, { name: 'fred' }];
Aigle.resolve(array)
.map('name') // the function is called asynchronously
.then(names => console.log(names)); // ['barney', 'fred']

Aigle.mapValues

The function is similar to Async.mapValues and Lodash.mapValues. It is also similar to Aigle.map, but it returns an object instead. Below is a code comparision between Aigle and Bluebird.

const object = { a: 1, b: 2, c: 3 };
const iterator = value => Promise.resolve(value * 2);
// Bluebird
const result = {};
Bluebird.each(Object.keys(object), key => {
const value = object[key];
return iterator(value)
.then(val => {
result[key] = val;
});
}).then(() => console.log(result)); // { a: 2, b: 4, c: 6 }
// Aigle
Aigle.mapValues(object, iterator)
.then(result => console.log(result)); // { a: 2, b: 4, c: 6 }

Aigle.times

The function is similar to Async.times and Lodash.times. It calls an iterator specified times.

const n = 10;
const iterator = value => Promise.resolve(value * 2);
// Bluebird
const times = [];
for (let i = 0; i < n; i++) {
times.push(i);
}
Bluebird.map(times, iterator)
.then(result => console.log(result)); // [0, 2, 4, ..., 18]
// Aigle
Aigle.times(n, iterator)
.then(result => console.log(result)); // [0, 2, 4, ..., 18]

Conclusion

Aigle is a promise library that provides safer, less verbose, and maintainable code that boasts impressive benchmarks. The documented examples above are only a subset of Aigle's functions. If you are already familiar with Lodash or Async, you will be able to easily transition into writing even more readable code with Aigle. With that said, letโ€™s enjoy writing faster and more efficient Javascript using Aigle.

Reference

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.