Published
Edited
Sep 13, 2021
3 stars
Also listed in…
Hello
Insert cell
Insert cell
sharedBuffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 1e7) // 4 bytes per item in a Float32Array
Insert cell
import {worker} from "@fil/worker"
Insert cell
Insert cell
C = navigator.hardwareConcurrency || 4
Insert cell
runWorkers = {
launchWorkers;
const seed = Math.random(),
time = performance.now();
await Promise.all(
Array.from(
{ length: C },
(_, start) =>
new Promise(notify =>
worker(compute, { seed, sharedBuffer, start, n: 1000, C })(notify)
)
)
);
return performance.now() - time;
}
Insert cell
(runWorkers, new Float32Array(sharedBuffer))
Insert cell
function compute(options) {
const sharedArray = new Float32Array(options.sharedBuffer),
n = options.n || 1000,
C = navigator.hardwareConcurrency || 4;
for (let i = options.start; i < sharedArray.length; i += C) {
for (
let a = 0;
a < n;
a++ // make it slow :D
)
sharedArray[i] = Math.cos(i / 100 + options.seed);
}
return "anything"; // a message saying we're done
}
Insert cell
Insert cell
Insert cell
normal = {
if (!launchNormal) return;
const seed = Math.random(),
time = performance.now();
for (let start = 0; start < C; start++)
compute({ seed, sharedBuffer, start, n: 1000, C });
return performance.now() - time;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more