Novices Plagiarizing Mozart
Have ideas to improve npm?Join in the discussion! »


1.1.7 • Public • Published


JavaScript module to parallel process data through dynamic multi-thread workers.


NPM or Yarn

The most common way used for the modern web applications development:

npm i -S @jsweb/worker

yarn add @jsweb/worker

And at your JS you can import the module that fits your environment:

// ES6+ frontend web dev to use worker at the browser
import worker from '@jsweb/worker'
// ES6+ backend dev to use worker in Node.js
import worker from '@jsweb/worker/dist/node.mjs'
// CommonJS backend dev to use worker in Node.js
const worker = require('@jsweb/worker/dist/node.js')

Yes, it works in Node.js, but you must to import/require the correct module for your dev environment.


If you prefer, you can get it directly from Unpkg CDN to use it in web frontend.

<script src=""></script>

By including the script in your page as traditional JS, the jsWebWorker function becomes available at the global scope.

This main exported module is in UMD format and targets browser.

But if you want to use a modern ES6+ module, you can do this:

<script type="module">
  import worker from ''
  // Your code goes here...

Usage example

import worker from '@jsweb/worker'
let result
const data = [...aLotOfItemsHere]
  .env('two', 2)
  .map((item) => item.value ** self.env.two)
  .reduce((value, sum) => value + sum, 0)
  .finally((sum) => (result = sum))


Notice the self thing into the code!

Any function task running at the Worker must refer to the environment as self to access other context resources, except for globals.

The worker


The worker function returns a high level object instance that works like Promise, with async methods.

In fact, it uses a Promise internaly to guide the entire process data flow when methods are called.

You can chain or async/await them to get the results.

The worker instance receive 2 arguments:

  1. data or value to process (required)
  2. a number to setup concurrent threads to process data (optional)
worker(data, 4).exec(...)

The worker tries to detect how much "hardware concurrency threads" are available to use, or use the default: 2.

Environment (scope)

At the browser, Workers run in parallel hardware threads, detached from main JS application thread.

It means you don't have access to all props and features available at the global JS scope.

If you need to know more about this, try to start here: Web Workers API : Functions and interfaces available in workers.

At the Node.js environment, workers use child_process. To know more about this, go to Node.js API: child_process


All methods are asyncronous and return the worker instance to chain other methods, except the .finally method, that ends the flow worker and returns just a native Promise.

.env(key, value)

Sets a Worker environment constant attached to 'self.env' object.

  • key must to be a valid object key
  • value can be any data to be available at Worker environment

Inside your tasks functions, you can refer to these constants through 'self.env' object.

  .env('x', 5)
  .env('y', 25)
  .exec((value) => value * self.env.x * self.env.y)


Imports code to use at Worker enviroment like functions or scripts.

Accepts any number of named functions or scripts URLs.

const moment = ''
function howManyTimeAgo(dt) {
  return self.moment(dt).fromNow()
function ageCalc({ birthDate }) {
  return self.howManyTimeAgo(birthDate)
worker(data).imports(moment, howManyTimeAgo).exec(ageCalc)

It's just a "dumb" example... But it's important to remember about Worker scope!


Import NPM modules from UNPKG CDN to use at Worker environment.

Accepts any number of string arguments to identify modules.

function createKeysForAllItems(item) {
  item.key = self.randkey.uuid()
  return item


Executes a task function in a single parallel thread.

  .env('x', 5)
  .env('y', 25)
  .exec((value) => value * self.env.x * self.env.y)


Executes a multi-thread method.

  .env('two', 2)
  .map((item) => item.value ** self.env.two)

.reduce(task, arg)

Executes a multi-thread Array.reduce method.

worker(data).reduce((value, sum) => value + sum, 0)


Executes a single parallel thread Array.filter method.

worker(data).filter((value) => value.includes('OK'))


Executes a single parallel thread Array.sort method.

worker(data).sort((a, b) => a - b)


Executes a multi-thread Array.flatMap method.

const data = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  // ...
  .env('two', 2)
  .faltMap((item) => item.value ** self.env.two)


Promise-like async step.

Unlike the native Promise.then, which returns a Promise, this method returns the worker instance to chain another methods.

  .filter((value) => value.includes('OK'))


Promise-like async last step.

Like the native Promise.finally, this method returns a Promise and stops the worker instance flow.

You can't chain other worker methods after this.

  .filter((value) => value.includes('OK'))

How this module works?

Coming soon...


npm i @jsweb/worker

DownloadsWeekly Downloads






Unpacked Size

66.1 kB

Total Files


Last publish


  • avatar