A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
Clone or download
Latest commit 9d52b5b Sep 20, 2018
Permalink
Failed to load latest commit information.
__tests__ rename test folder Sep 20, 2018
docs regenerate docs Sep 20, 2018
public add doc images Sep 10, 2018
src regenerate docs Sep 20, 2018
.bookignore add book ignore Sep 20, 2018
.gitignore ignore dist folder Sep 12, 2018
.npmignore ignore bookignore Sep 20, 2018
.travis.yml add report coverage as final step Sep 20, 2018
API.md use generics Sep 20, 2018
GUIDE.md add guide doc Sep 17, 2018
LICENSE Initial commit Aug 26, 2018
README.md add npm package badge Sep 20, 2018
SUMMARY.md add Guide link Sep 17, 2018
index.d.ts config is optional Sep 17, 2018
package-lock.json 0.2.1 Sep 20, 2018
package.json 0.2.1 Sep 20, 2018
tsconfig.json deactivate source maps Sep 17, 2018
tslint.json remove annoying errors Sep 17, 2018

README.md

Microjob

npm version Build Status Coverage Status

A tiny wrapper for turning Node.js threads in easy-to-use routines for CPU-bound.

When my multithreaded program works like a charm

When my multithreaded program works like a charm - thecodinglove

Introduction

Microjob is a tiny wrapper for Node.js threads and is intended to perform heavy CPU loads using anonymous functions. So, Microjob treats Node.js threads as temporary working units: if you need to spawn a long-living thread, then you should use the default API.

Microjob follows the same line of the original Node.js documentation: use it only for CPU-bound jobs and not for I/O-bound purposes. Quoting the documentation:

Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can.

Microjob can be used only with Node.js 10.5+ and with the --experimental-worker flag activated, otherwise it won't work.

More details explained in: Microjob: a tiny multithreading library for Node.js

Installation

Via npm:

$ npm install --save microjob

Quick Example

(async () => {
  const { job, stop } = require('microjob')

  try {
    // this function will be executed in another thread
    const res = await job(() => {
      let i = 0
      for (i = 0; i < 1000000; i++) {
        // heavy CPU load ...
      }

      return i
    })

    console.log(res) // 1000000
  } catch (err) {
    console.error(err)
  }

  // graceful shutdown
  stop()
})()

Documentation

Dive deep into the documentation to find more examples: Guide

Known Issues

Known Limitations