nonblocking
v1.0.3
Published
asynchronous Array.prototype functions that won't block on large arrays
Downloads
966
Maintainers
Readme
nonblocking
Array.prototype functions that won't block on large arrays
usage
var nonblocking = require('nonblocking')
var bigArray = []
for (var i; i < 10000000; i++) {
bigArray.push(i)
}
nonblocking(bigArray).forEach(function (el) {
console.log(el)
}, function (err) {
console.log('all done!')
})
When iterating over large arrays, even simple functions may become blocking. In a browser, this might mean an slow, "janky" page. In Node.js, this might mean a completely non-responsive web server!
Use this module to fan out over a large array. Note
that the functions being applied to each element are
not treated asynchronously or in parallel, so it's
not suited to file system or network IO. If that's
what you want, consider the popular async
module.
Measure before optimizing! You can use the event-loop-lag
module to measure if you have anything blocking in your Node.js process. If those numbers start
to rise, nonblocking
might help you!
api
In brief, call any of filter
, forEach
, some
, every
, or map
, passing the array as the first arugment, and a Node.js-style callback as the last.
The methods are also available in a fluent syntax like lodash or underscore. Use whichever one you prefer.
Using jsig annotation.
filter
and forEach
also accept Dictionary objects. Other functions may support operating on Dictionaries in the future
forEach(arr: Array | Dictionary, fn: Function) => Callback<>
The final callback parameter is optional.
e.g.
nonblocking.forEach([1, 2, 3], function (x) {
console.log(x)
}, function (err) {
// logged: 1\n2\n3
})
// or
nonblocking([1, 2, 3]).forEach(function (x) {
console.log(x)
}, function (err) {
// logged: 1\n2\n3
})
// Dictionary usage
nonblocking({a: 1, b: 2, c: 3}).forEach(function (val, key) {
console.log(key, val)
}, function (err) {
// logged: "a" 1\n"b" 2\n"c" 3
})
filter(arr: Array | Dictionary, fn: Predicate) => Callback<Array>
e.g.
nonblocking.filter([1, 2, 3], function (x) { return x >= 2 }, function (err, out) {
// out = [2, 3]
})
// or
nonblocking([1, 2, 3]).filter(function (x) { return x >= 2 }, function (err, out) {
// out = [2, 3]
})
// Dictionary usage
nonblocking({a: 1, b: 2, c: 3]).filter(function (val, key) { return val >= 2 }, function (err, out) {
// out = {b: 2, c: 3}
})
map(arr: Array, fn: Function) => Callback<Array>
e.g.
nonblocking.map([1, 2, 3], function (x) { return x + 1 }, function (err, out) {
// out = [2, 3, 4]
})
// or
nonblocking([1, 2, 3]).map(function (x) { return x + 1 }, function (err, out) {
// out = [2, 3, 4]
})
some(arr: Array, fn: Predicate) => Callback<Boolean>
e.g.
nonblocking.some([1, 2, 3], function (x) { return x < 0 }, function (err, out) {
// out = false
})
// or
nonblocking([1, 2, 3]).some(function (x) { return x < 0 }, function (err, out) {
// out = false
})
every(arr: Array, fn: Predicate) => Callback<Boolean>
e.g.
nonblocking.every([1, 2, 3], function (x) { return x > 0 }, function (err, out) {
// out = true
})
// or
nonblocking([1, 2, 3]).every(function (x) { return x > 0 }, function (err, out) {
// out = true
})
installation
$ npm install nonblocking
running the tests
From package root:
$ npm install
$ npm test
contributors
- jden [email protected]
license
ISC. (c) MMXVI jden [email protected]. See LICENSE.md