What is neo-async?
The neo-async package is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. It is similar to the async package but with some performance improvements.
What are neo-async's main functionalities?
Control Flow
Execute a series of functions in sequential order. Each function is passed a callback it must call on completion.
async.series([
function(callback) {
// do some stuff ...
callback(null, 'one');
},
function(callback) {
// do some more stuff ...
callback(null, 'two');
}
],
function(err, results) {
// results is now equal to ['one', 'two']
});
Collections
Apply a function to each item in a collection and collect the results.
async.map(['file1','file2','file3'], fs.stat, function(err, results) {
// results is now an array of stats for each file
});
Utilities
Call a function a certain number of times and collect the results.
async.times(5, function(n, next) {
createUser(n, function(err, user) {
next(err, user);
});
}, function(err, users) {
// we should now have 5 users
});
Other packages similar to neo-async
async
The original async package offers a wide array of functions for working with asynchronous code. Neo-async claims to offer similar functionality with improved performance.
bluebird
Bluebird is a full-featured promise library with a focus on innovative features and performance. It can be used as an alternative to neo-async for handling asynchronous operations using promises instead of callbacks.
q
Q is a tool for making and composing asynchronous promises in JavaScript. It's an older promise library that can serve similar purposes to neo-async but with a different style of handling async operations.
Neo-Async
Neo-Async is compatible with Async.js, it is faster and has more feature.
Async is a utilty module which provides staright-forward.
Installation
In a browser
<script src="async.min.js"></script>
In an AMD loader
require(['async.min'], function(async) {});
Node.js
standard
$ npm install neo-async
var async = require('neo-async');
replacement
$ npm install neo-async
$ ln -s ./node_modules/neo-async ./node_modules/async
var async = require('async');
Feature
Collections
- async.each [Series, Limit]
- async.map [Series, Limit]
- async.filter [Series, Limit]
- async.reject [Series, Limit]
- async.detect [Series, Limit]
- async.pick [Series, Limit]
- async.transform [Series, Limit]
- async.reduce
- async.reduceRight
- async.sortBy [Series, Limit]
- async.some [Series, Limit]
- async.every [Series, Limit]
- async.concat [Series, Limit]
Control Flow
- async.series
- async.parallel [Limit]
- async.waterfall
- async.whilst
- async.doWhilst
- async.until
- async.doUntil
- async.forever
- async.seq
- async.applyEach [Series]
- async.queue
- async.priorityQueue
- async.cargo
- async.auto
- async.retry
- async.iterator
- async.nextTick
- async.setImmediate
- async.times [Series, Limit]
Utils
- async.memoize
- async.unmemoize
- async.log
- async.dir
- async.noConflict
Speed Comparison
sample script
sample.waterfall.js
'use strict';
var comparator = require('func-comparator');
var _ = require('lodash');
var async = require('async');
var neo_async = require('neo-async');
var count = 10;
var times = 10000;
var array = _.sample(_.times(count), count);
var tasks = _.map(array, function(n, i) {
if (i === 0) {
return function(next) {
next(null, n);
};
}
return function(total, next) {
next(null, total + n);
};
});
var funcs = {
'async': function(callback) {
async.waterfall(tasks, callback);
},
'neo-async': function(callback) {
neo_async.waterfall(tasks, callback);
}
};
comparator
.set(funcs)
.option({
async: true,
times: times
})
.start()
.result(function(err, res) {
console.log(res);
});
execute
$ node --stack-size=65536 speed_test/sample.waterfall.js
result
{ async:
{ min: 47.16,
max: 7957.74,
average: 69.31,
variance: 31939.14,
standard_deviation: 178.71,
vs: { 'neo-async': 12.03 } },
'neo-async':
{ min: 3.2,
max: 7296.46,
average: 8.34,
variance: 11866,
standard_deviation: 108.93,
vs: { async: 831.05 } } }
Results
- waterfall
- parallel
- parallelLimit
- concat
- series
- vs: { async: 52.14 } ~ vs: { async: 779.62 }
- unstable: because sample is less.