Last active
December 16, 2017 20:37
-
-
Save kesne/9fd184a767d894f4c4f6e5037e2a9a4f to your computer and use it in GitHub Desktop.
better-worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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