All Projects β†’ developit β†’ Greenlet

developit / Greenlet

🦎 Move an async function into its own thread.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Greenlet

Thread
type safe multi-threading made easier
Stars: ✭ 34 (-99.25%)
Mutual labels:  worker, thread, webworker
Workly
A really simple way to move a function or class to a web worker. πŸ‹οΈβ€β™€οΈβ†’ πŸ˜„
Stars: ✭ 1,848 (-59.03%)
Mutual labels:  thread, web-worker, webworker
Alloy Worker
ι’ε‘δΊ‹εŠ‘ηš„ι«˜ε―η”¨ Web Worker ι€šδΏ‘ζ‘†ζžΆ
Stars: ✭ 349 (-92.26%)
Mutual labels:  thread, web-worker, worker
Hamsters.js
100% Vanilla Javascript Multithreading & Parallel Execution Library
Stars: ✭ 517 (-88.54%)
Mutual labels:  thread, web-worker, worker
Workerize Loader
πŸ—οΈ Automatically move a module into a Web Worker (Webpack loader)
Stars: ✭ 2,135 (-52.67%)
Mutual labels:  web-worker, worker
Redux In Worker
Entire Redux in Web Worker
Stars: ✭ 168 (-96.28%)
Mutual labels:  web-worker, worker
React Native Threads
Create new JS processes for CPU intensive work
Stars: ✭ 527 (-88.32%)
Mutual labels:  thread, web-worker
Useworker
βš›οΈ useWorker() - A React Hook for Blocking-Free Background Tasks
Stars: ✭ 2,233 (-50.5%)
Mutual labels:  thread, web-worker
Preact Worker Demo
Demo of preact rendering an entire app in a Web Worker.
Stars: ✭ 204 (-95.48%)
Mutual labels:  thread, web-worker
Post Me
πŸ“© Use web Workers and other Windows through a simple Promise API
Stars: ✭ 398 (-91.18%)
Mutual labels:  web-worker, worker
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (-99.14%)
Mutual labels:  worker, webworker
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (-99.6%)
Mutual labels:  worker, thread
Worker Plugin
πŸ‘©β€πŸ­ Adds native Web Worker bundling support to Webpack.
Stars: ✭ 1,840 (-59.21%)
Mutual labels:  web-worker, webworker
Stockroom
πŸ—ƒ Offload your store management to a worker easily.
Stars: ✭ 1,745 (-61.32%)
Mutual labels:  web-worker, worker
Remote Web Streams
Web streams that work across web workers and iframes.
Stars: ✭ 26 (-99.42%)
Mutual labels:  web-worker, worker
Wasm Worker
Move a WebAssembly module into its own thread
Stars: ✭ 215 (-95.23%)
Mutual labels:  thread, web-worker
tgrid
TypeScript Grid Computing Framework supporting RFC (Remote Function Call)
Stars: ✭ 83 (-98.16%)
Mutual labels:  worker, thread
partytown
Relocate resource intensive third-party scripts off of the main thread and into a web worker. πŸŽ‰
Stars: ✭ 3,626 (-19.62%)
Mutual labels:  web-worker, webworker
Parallel.js
Easy multi-core processing utilities for Node.
Stars: ✭ 3,096 (-31.37%)
Mutual labels:  webworker
Transmittable Thread Local
πŸ“Œ TransmittableThreadLocal (TTL), the missing Javaβ„’ std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.
Stars: ✭ 4,678 (+3.7%)
Mutual labels:  thread

Greenlet

Greenlet npm travis gzip size install size

Move an async function into its own thread.

A simplified single-function version of workerize, offering the same performance as direct Worker usage.

The name is somewhat of a poor choice, but it was available on npm.

Greenlet supports IE10+, since it uses Web Workers. For NodeJS usage, Web Workers must be polyfilled using a library like node-webworker.

Installation & Usage

npm i -S greenlet

Accepts an async function with, produces a copy of it that runs within a Web Worker.

⚠️ Caveat: the function you pass cannot rely on its surrounding scope, since it is executed in an isolated context.

greenlet(Function) -> Function

‼️ Important: never call greenlet() dynamically. Doing so creates a new Worker thread for every call:

-const BAD = () => greenlet(x => x)('bad') // creates a new thread on every call
+const fn = greenlet(x => x);
+const GOOD = () => fn('good'); // uses the same thread on every call

Since Greenlets can't rely on surrounding scope anyway, it's best to always create them at the "top" of your module.

Example

Greenlet is most effective when the work being done has relatively small inputs/outputs.

One such example would be fetching a network resource when only a subset of the resulting information is needed:

import greenlet from 'greenlet'

let getName = greenlet( async username => {
    let url = `https://api.github.com/users/${username}`
    let res = await fetch(url)
    let profile = await res.json()
    return profile.name
})

console.log(await getName('developit'))

πŸ”„ Run this example on JSFiddle

Transferable ready

Greenlet will even accept and optimize transferables as arguments to and from a greenlet worker function.

Browser support

Thankfully, Web Workers have been around for a while and are broadly supported by Chrome, Firefox, Safari, Edge, and Internet Explorer 10+.

If you still need to support older browsers, you can just check for the presence of window.Worker:

if (window.Worker) {
    ...
} else {
    ...
}

CSP

If your app has a Content-Security-Policy, Greenlet requires worker-src blob: and script-src blob: in your config.

License & Credits

In addition to the contributors, credit goes to @sgb-io for his annotated exploration of Greenlet's source. This prompted a refactor that clarified the code and allowed for further size optimizations.

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].