Node.js is (sorta) single threaded and optimized for I/O not CPU bound operations.
For large collections and calculation intensive operations functions such as each
or map
provided by utility libraries like underscore or lodash can monopolize the CPU, blocking Node's event loop.
Besides spawning child processes, using web workers, etc it makes sense to avoid synchronous fns.
Install with npm: npm i -S a_
Run unit tests with: npm test
Use in your project:
import _ from 'a_'
// or
const _ = require('a_')
The _ implementations here work similar to underscore's implementations,
but accept a callback or return a promise alternatively:
_.each(collection, iterator, callback)
_.each(collection, iterator)
_.filter(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.map(collection, iterator, callback)
_.map(collection, iterator)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.filter(collection, test, callback)
_.filter(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.reject(collection, test, callback)
_.reject(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.reduce(collection, reducer, accumulator, callback)
_.reduce(collection, reducer, callback)
_.reduce(collection, reducer, accumulator)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.reduce(collection, reducer)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.every(collection, test, callback)
_.every(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
_.some(collection, test, callback)
_.some(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
Returns a slice of array excluding elements dropped from the beginning.
Elements are dropped until test returns falsey
_.some(collection, test, callback)
_.some(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
Iterates over collection until iterator first returns truthy, returns index or key for value for which iterator returned truthy.
_.eachUntil(collection, test, callback)
_.eachUntil(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
Iterates over collection until iterator first returns falsy, returns index or key for value for which iterator returned falsy.
_.eachWhile(collection, test, callback)
_.eachWhile(collection, test)
.then((result) => console.log(result))
.catch((err) => console.error(err))
Iterates over collection and returns first item to be found returning truthy on the iterator function.
_.find(collection, iterator, callback)
_.find(collection, iterator)
.then((result) => console.log(result))
.catch((err) => console.error(err))
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.