npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cooperative

v1.1.1

Published

cooperative threading versions of map, filter, forEach, etc, suitable for big processing in single-threaded Node.js

Downloads

1,481

Readme

cooperative npm version npm Build Status

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

map

let mappedResults = await cooperative.map(array, mapper, options)
  • array - an array
  • mapper(item, index) - a function taking each array item and index, and returning either a value or a Promise of a value.
  • mappedResults - the corresponding results of mapper for each item in array

filter

let filteredArray = await cooperative.filter(array, predicate, options)
  • array - an array
  • predicate(item, index) - a function taking each array 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 the
  • filteredArray - the items in array for which predicate returned a truthy value

forEach

await cooperative.forEach(array, action, options)
  • array - an array
  • action(item, index) - a function taking each array item and index and performing an action. If the return value is a promise, then forEach will wait for all promises to complete.

reduce

let reducedResults = await cooperative.reduce(array, operator, initial, options)
  • array - an array
  • operator(accumulator, item, index) - a function taking an accumulator, each array item and index. The return value is the accumulator for the operator call on the next item.
  • initial - the value of the first accumulator
  • reducedResults - the last accumulator

mapObject

let mappedObject = await cooperative.mapObject(object, mapper, options)
  • object - an object
  • mapper(value, key) - a function taking each value and key in object, the value returned is placed into a new object at key. This function can return a promise.
  • mappedObject - a new object with it's values mapped by mapper

Options

  • options.interval - the amount of time in ms to allow processing before calling setInterval, defaults to 10ms.

We're Hiring!

Featurist provides full stack, feature driven development teams. Want to join us? Check out our career opportunities.