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

worker-function

v2.0.2

Published

Create functions executed within a web worker and return promises

Downloads

1,725

Readme

worker-function

Create functions that are executed inside of web workers and return promises.

Allows to create inline web workers without the need of creating new files for them. Have a look at the example:

const WorkerFunction = require('worker-function');

// Let's create a new worker
var workerSum = WorkerFunction( function( arg1, arg2, done ){
  // Worker execution can be async,
  // don't forget to call `done`
  setTimeout( () => done( arg1 + arg2 ), 2000 );
});

// workerSum is used in a new thread
// and return a promise with the result
workerSum(2, 3).then( result => {
  console.log( result ); // 5
});

See it working in a JSBin.

worker-function reinforce the usage of web workers as disposable resources. In the example, everytime workerSum is called, a new web worker is created to execute the function in its own thread. When the function done is called the web worker is terminated freeing memory.

The library is really lightweight, less than 1Kb minified.

Installation

npm install worker-function

Or you can use WorkerFunction.js directly in the browser.

usage

Functions to be executed in a web worker are created by passing them to the WorkerFunction:

var workerFn = WorkerFunction( function Fn(arg1,arg2,...,argN,done){
  // Calling done will resolve the promise with the result given
  done( 'Hey there' );
});

Now it's possible to call workerFn in the usual way, but it will be executed in its own thread, within a web worker. The execution will be isolated from the main browser thread so you can't use any of the variables defined outside of the function.

It's possible to pass any number of arguments needed to a worker function. In addition, done function is always passed as the last argument, and it's mandatory to call it to send the result from the main thread, resolving the promise and terminating the worker:

workerFn(arg1, arg2, ..., argN)
  .then( result => {
    console.log( result ); // 'Hey there'
  })
  .catch( err => {
    // Any error inside the worker execution can
    // be catched using the Promise's catch method
    console.error( err );
  })
;

Debugging

You can use your browser's dev tools to debug your worker functions. Try to add debugger to your function and the debugger will stop there:

var wf = WorkerFunction( done => {
  debugger;
  done('This function was stopped in the previous line.');
});

Compatibility

Most of modern browsers support web workers, see compatibility list.

But in case we need to run the code in browsers that don't support them, or in Node environments where web workers are not available, worker-function falls back running the functions in the main thread, so we can use the library with no compromise.

The only requirement of worker-function to work is to have Promises available. So if we need our code to be compatible with old browers, we need to get sure we polyfill promises.

Performance

worker-function treat web workers as disposable resources, so there is some time spent when we start a worker up.

Fortunatelly, that startup time is almost unperceivable in the modern browsers. In our benchmarks, running a worker-function scores almost exactly the same than running the function in the body of the Promise.resolve method:

Chrome 81
---
Worker function x 67.73 ops/sec ±2.81% (56 runs sampled)
Promise function x 68.78 ops/sec ±2.36% (50 runs sampled)

Firefox 75
---
Worker function x 182 ops/sec ±2.11% (54 runs sampled)
Promise function x 182 ops/sec ±2.43% (55 runs sampled)

Safari 13
---
Worker function x 307 ops/sec ±1.61% (57 runs sampled)
Promise function x 318 ops/sec ±1.29% (59 runs sampled)

These benchmarks are available for anyone to run at test.html.

License

Mit © Javier Marquez