Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kanitsharma/workinator
Run your CPU intensive functions in a separate thread on the fly
https://github.com/kanitsharma/workinator
javascript multithreading performance threads webworkers
Last synced: 20 days ago
JSON representation
Run your CPU intensive functions in a separate thread on the fly
- Host: GitHub
- URL: https://github.com/kanitsharma/workinator
- Owner: kanitsharma
- Created: 2019-03-31T11:38:55.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-10-31T11:09:56.000Z (about 5 years ago)
- Last Synced: 2023-09-18T14:23:12.350Z (about 1 year ago)
- Topics: javascript, multithreading, performance, threads, webworkers
- Language: JavaScript
- Homepage:
- Size: 252 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
Workinator
> Run your CPU intensive functions in a separate thread on the fly, and keep your application running at 60FPS.
- Works on both Browser or Nodejs
- Minimal API
- Tiny package, ~1KB gzipped
- Supports both synchronous or asynchronous code.
- Automatically cleans up memory after worker thread is finished executing.## Getting Started
```javascript
yarn add @kimera/workinator
// or
npm i @kimera/workinator
```## How it works
### Basic
```javascript
import workinator from '@kimera/workinator';workinator(() => {
console.log('Hello from worker');
})// Thats it!.
```### Synchronous
```javascript
import workinator from '@kimera/workinator';const work = () => {
// blocking thread for 2 secs
const start = new Date().getTime();
while (new Date().getTime() < start + 2000) {}return 'Work finished';
};const main = async () => {
const status = await workinator(work);
console.log(status);
};main();
```## Async with promises
```javascript
import workinator from '@kimera/workinator';workinator(
() =>
new Promise(resolve => {
setTimeout(() => {
resolve('Work Finished');
}, 2000);
}),
).then(console.log);// or
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
workinator(async () => {
await sleep(2000);
return 'Work Finished';
}).then(console.log);
```## Using Dependencies
Worker functions inside workerinator does not allow using closures, since its executed inside a different thread. So, instead what we can do is inject these dependencies as the second argument of workerinator and you will receive the dependencies as arguments inside worker function in their respective order.
### Example
```javascript
import workinator from '@kimera/workinator';const log = x => console.log(x);
workinator(
logger =>
new Promise(resolve => {
logger('Dependency working');
}),
log,
)
```