Skip to content
/ microjob Public

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.

License

Notifications You must be signed in to change notification settings

wilk/microjob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

00e1770 Β· Jan 28, 2021
Mar 12, 2020
Oct 15, 2019
Oct 20, 2019
Sep 10, 2018
Mar 12, 2020
Oct 16, 2019
Apr 18, 2019
Oct 16, 2019
Nov 4, 2018
Oct 16, 2019
Jul 29, 2019
Sep 20, 2018
Apr 18, 2019
Apr 18, 2019
Aug 26, 2018
Mar 13, 2020
Sep 17, 2018
Oct 20, 2019
Oct 20, 2019
Jan 12, 2019
Nov 13, 2018
Jul 17, 2019

Repository files navigation

Microjob

npm version Build Status Coverage Status Dependencies

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

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.

From version v0.1.0 microjob uses a Worker Pool πŸŽ‰

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 with Node.js 12+ without flag. With Node.js 10.5+ you need 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, start, stop } = require("microjob");

  try {
    // start the worker pool
    await start();

    // 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);
  } finally {
    // shutdown worker pool
    await stop();
  }
})();

Features

  • πŸ›’οΈ Worker Pool
  • πŸ₯ auto self-healing
  • πŸ™Œ easy and simple
  • πŸ•” supports both sync and async jobs
  • πŸ›‘οΈ huge test coverage
  • πŸ“œ well documented

Documentation

Dive deep into the documentation to find more examples: Guide

Known Issues

Known Limitations