Skip to content

Instantly share code, notes, and snippets.

<script>
/*
thread() is a function that allows you to execute any isolated function asynchronously in a worker thread.
await thread(myFunction, [arg1, arg2, ...])
See more examples and tests below using async functions, transfers, aborts, concurrency,.
*/
function thread(func, args, options) {
@jozefchutka
jozefchutka / test.js
Created May 30, 2022 07:15
setTimeout vs requestAnimationFrame vs scheduler vs Promise vs queueMicrotask
setTimeout(() => console.log("setTimeout #1"))
requestAnimationFrame(() => console.log("requestAnimationFrame #1"))
scheduler.postTask(() => console.log("scheduler #1"))
queueMicrotask(() => console.log("queueMicrotask #1"))
Promise.resolve().then(() => console.log("Promise #1"))
setTimeout(() => console.log("setTimeout #2"))
requestAnimationFrame(() => console.log("requestAnimationFrame #2"))
scheduler.postTask(() => console.log("scheduler #2"))
queueMicrotask(() => console.log("queueMicrotask #2"))
@jozefchutka
jozefchutka / SRJson.js
Created May 6, 2022 11:58
Self-referencing JSON to handle recursion, WDYT?
function stringify(value) {
const map = new Map();
function traverse(value, reference) {
if(map.has(value))
return map.get(value);
if(Array.isArray(value)) {
map.set(value, reference);
let result = "";
requestAnimationFrame(() => console.log(1));
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
requestAnimationFrame(() => console.log(4));
setTimeout(() => console.log(5), 0);
Promise.resolve().then(() => console.log(6));
// 3, 6, 2, 5, 1, 4