Aigle vs Bluebird

Written by suguru.motegi | Published 2017/05/02
Tech Story Tags: javascript | es6 | promises | nodejs | aigle

TLDRvia the TL;DR App

bench parallel

[Aigle](https://github.com/suguru03/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](https://github.com/lodash/lodash) and [Async](https://github.com/caolan/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 ensuredreturn Bluebird.delay(10);}, { concurrency: 2 });

Aigle.mapLimit(array, 2, value => {console.log(value); // 1, 2, 3, 4, 5 the order is ensuredreturn Aigle.delay(10);});

// you could use default limit, the limit is 8Aigle.mapLimit(array, value => {console.log(value); // 1, 2, 3, 4, 5return 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);

// Bluebirdconst 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 }

// AigleAigle.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);

// Bluebirdconst times = [];for (let i = 0; i < n; i++) {times.push(i);}Bluebird.map(times, iterator).then(result => console.log(result)); // [0, 2, 4, ..., 18]

// AigleAigle.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


Published by HackerNoon on 2017/05/02