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

background-functions

v1.0.6

Published

Execute CPU intensive code asynchronously in child processes Edit

Downloads

22

Readme

Background Functions

What?

This module provides a way of moving blocking/longish CPU-bound tasks out of Node's event loop to JavaScript threads that run in parallel in the background and that use all the available CPU cores automatically; all from within a single Node process.

Why?

After the initialization phase of a Node program, whose purpose is to setup listeners and callbacks to be executed in response to events, the next phase, the proper execution of the program, is orchestrated by the event loop whose duty is to juggle events, listeners and callbacks quickly and without any hiccups nor interruptions that would ruin its performance.

Both the event loop and said listeners and callbacks run sequentially in a single thread of execution, Node's main thread. If any of them ever blocks, nothing else will happen for the duration of the block: no more events will be handled, no more callbacks nor listeners nor timeouts nor setImmediate()ed functions will have the chance to run and do their job, because they won't be called by the blocked event loop, and the program will turn sluggish at best, or appear to be frozen and dead at worst.

How?

npm install background-functions --save
const WorkersMaster = require('background-functions');

var Workers = new WorkersMaster({
    requires: { 
        debug: 'debug' // Inject dependencies (npm modules) 
    },
    threads: 2 // Defaults to number of CPU's
});

// Example 1
var exampleTask = Workers.task(function(n) {
    for (var i = 0; i < n; ++i)
    {
        // Any CPU intensive task
    }
    
    return 'Hello';
});

exampleTask(10000000).then(function(result) {
    console.log(result); // Output: Hello
});


// Example 2
 
var fibo = Workers.task(function(n) {
    debug('task')('task executing', n); // `debug` injected
    
    var fibo = function(n) {
        return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
    };
    
    // Return promise or any other native js data type
    return new Promise(function(resolve, reject) {
        resolve(fibo(n));
    })
});

// `Workers.task` always returns Promise
fibo(40).then(function(result) {
    console.log('fibo(40) = ', result);
});

Limitations?

  • no browser support.
  • only supports function arguments which can be serialised to json.
  • makes sense only when overhead of IPC and serialisation is much less than code execution.

Alternatives?

How does it work?

It creates nodejs child processes and sends them serialised functions using IPC. When functions are called it sends json serialised function arguments to one of the thread in round robin fasion and returns a promise.