-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
27 lines (23 loc) · 826 Bytes
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const MODULES = {};
async function get_cached_module(modname) {
if (MODULES[modname] === undefined) {
MODULES[modname] = await import(`/worker.${modname}.js`);
}
const mod = MODULES[modname]?.module || MODULES[modname];
console.log(mod);
return mod;
}
async function bench(test, runtime, callback, args = []) {
for (let i = 0; i < 10; i++) {
const start = performance.now();
await callback(...args);
const time = performance.now() - start;
self.postMessage(JSON.stringify({ test, runtime, time }));
}
}
self.addEventListener("message", async (msg) => {
const { test, runtime, args } = JSON.parse(msg.data);
const module = await get_cached_module(runtime);
await bench(test, runtime, module[test], args);
self.postMessage("finished");
});