Microjob

microjob

worker threads into easy-to-use routines for CPU-bound.

microjob is a tiny wrapper for threads (worker_threads) and is intended to perform heavy CPU loads using anonymous functions.

GET IT: npm install microjob

Sample usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(async () => {
  const { job } = 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)
  }
})();

P.S: To run this today, you we need node v>=10 and execute it via --experimental-worker flag.

GIF FTW!

microjob

Suggest a module

Comments