Skip to content
forked from wilk/microjob

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

Bin-Huang/microjob

 
 

Repository files navigation

Microjob

Build 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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.2%
  • JavaScript 3.8%