
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
cooperative
Advanced tools
cooperative threading versions of map, filter, forEach, etc, suitable for big processing in single-threaded Node.js
Cooperative threading versions of map, filter, forEach, etc, suitable for big processing in single-threaded Node.js.
One of the gotchas with Node.js is that it's single-threaded. Although the advantages of doing concurrency in a single-threaded environment far outstrip the disadvantages, there are still times when you want to do some lengthy processing but don't want to block up the thread for new HTTP requests or UI activity.
This module provides common functional primitives like map
, filter
and forEach
but that call setImmediate
regularly so as not to block other activity. This means you can do large processing and stay responsive.
The trick is to call setImmediate regularly so your application is still responsive, but not on every iteration so the processing isn't too slow. This module does that by processing the arrays and calling setImmediate once every 10ms or so, not too much, not too little. This interval is configurable of course.
const cooperative = require('cooperative')
let veryLargeArray = [1, 2, 3, ...]
let resultsPromise = cooperative.map(veryLargeArray, (item, index) => {
// some involved operation
})
// continue to process other IO, UI events, etc
let results = await resultsPromise
let mappedResults = await cooperative.map(array, mapper, options)
item
and index
, and returning either a value or a Promise of a value.mapper
for each item
in array
let filteredArray = await cooperative.filter(array, predicate, options)
item
and index
, and returning either a value or a Promise of a value. If the value is truthy then the item is returned in thearray
for which predicate
returned a truthy valueawait cooperative.forEach(array, action, options)
item
and index
and performing an action. If the return value is a promise, then forEach
will wait for all promises to complete.let reducedResults = await cooperative.reduce(array, operator, initial, options)
accumulator
, each array item
and index
. The return value is the accumulator for the operator
call on the next item
.let mappedObject = await cooperative.mapObject(object, mapper, options)
value
and key
in object
, the value returned is placed into a new object at key
. This function can return a promise.mapper
setInterval
, defaults to 10ms.FAQs
cooperative threading versions of map, filter, forEach, etc, suitable for big processing in single-threaded Node.js
The npm package cooperative receives a total of 173 weekly downloads. As such, cooperative popularity was classified as not popular.
We found that cooperative demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.