Skip to content

Instantly share code, notes, and snippets.

@kesne
Last active December 16, 2017 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kesne/9fd184a767d894f4c4f6e5037e2a9a4f to your computer and use it in GitHub Desktop.
Save kesne/9fd184a767d894f4c4f6e5037e2a9a4f to your computer and use it in GitHub Desktop.
better-worker
import BetterWorker from 'better-worker';
const bgSort = new BetterWorker((threads) => {
return threads.sort((threadA, threadB) => {
return threadA.id > threadB.id ? 1 : -1;
});
});
// Using the worker-ified function:
const threads = [{ id: 1 }, { id: 2 }];
bgSort(threads).then(sortedThreads => {
// Depending on the browser, sortedThreads was run in a background worker!
});
import BetterWorker from 'better-worker';
const lazyUpdate = new BetterWorker(() => {
// Do something we don't really care about...
}, {
// Workers have serialization costs that block the main thread, so a very low priority
// update can be deferred until the browser isn't doing anything.
priority: 'low',
// If workers aren't available, then don't bother running this:
inline: 'never',
});
lazyUpdate().then(() => {
// Lazy update occured
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment