https://github.com/joway/node-routine
A library to implement Goroutine-Like API with worker_threads.
https://github.com/joway/node-routine
Last synced: about 1 year ago
JSON representation
A library to implement Goroutine-Like API with worker_threads.
- Host: GitHub
- URL: https://github.com/joway/node-routine
- Owner: joway
- Created: 2019-08-26T13:05:04.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-03-25T05:59:30.000Z (about 4 years ago)
- Last Synced: 2024-10-14T15:32:58.221Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://routine.joway.io
- Size: 1.22 MB
- Stars: 43
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-routine

[](https://www.npmjs.com/package/node-routine)
[](https://www.npmjs.com/package/node-routine)
node-routine is a library to implement [Goroutine-Like API](https://gobyexample.com/goroutines) with [worker_threads](https://nodejs.org/api/worker_threads.html).
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](https://routine.joway.io)
## Architecture

## Requirement
- Nodejs >= 11.7
- Nodejs >= 10.5 with `--experimental-worker` flag
## Install
```shell
npm install -S node-routine
```
## Quick Example
```javascript
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
- [node-routine](https://github.com/joway/node-routine)
- [microjob](https://github.com/wilk/microjob)
[Benchmark Code](bench/job.ts)
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)
```