0.2.1 • Published 5 years ago

node-routine v0.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

node-routine

npm npm

node-routine is a library to implement Goroutine-Like API with worker_threads.

Compared to using the worker threads low level API directly, node-routine can make your codes more elegantly, like:

await go(() => (Math.random()))

Documentation

Documentation

Architecture

npm.io

Requirement

  • Nodejs >= 11.7
  • Nodejs >= 10.5 with --experimental-worker flag

Install

npm install -S node-routine

Quick Example

const { go, init, shutdown } = require('node-routine')

// init a worker threads pool
init({
  maxWorkerThreads: 2,
})

async function calc() {
  // every routine will be executed in worker threads pool
  const count = 10000
  const num = await go(() => {
    let total = 0
    for (let i = 0; i < count; ++i) {
      total += i
    }
    return total
  }, { count })

  return num
}

calc().then((total) => {
  console.log('Got', total)
  shutdown()
})

Benchmark

Benchmark Code

Env: Macbook Pro, 13-inch, 2018, 2.3 GHz Intel Core i5

Commend: npm run bench

  ✓ CPU intensive task using microjob (14ms)
  ✓ CPU intensive task using node-routine (4ms)
  ✓ IO intensive task using microjob (20163ms)
  ✓ IO intensive task using node-routine (5224ms)
0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago