examples(wasm): add a browser parallel-assets demo via Web Workers
Close the final "parallel assets" cell of the cross-language matrix for
WASM.
* examples/wasm/parallel_assets.html — generates a synthetic
`(assets, bars)` panel deterministically (the LCG matches the Node
and Rust siblings so timings are directly comparable), runs the
serial baseline on the main thread, then dispatches the same workload
to a pool of module Workers and reports the speedup. The render is
three cards (serial / parallel / speedup) plus a sanity-check line
asserting per-asset agreement between the two paths.
* examples/wasm/parallel_worker.js — companion module worker that
loads its own copy of the WebAssembly module via `init()` and
processes whichever slice of the panel its parent dispatches.
Modern browsers ship module-worker support (`new Worker(url, {
type: "module" })`) which lets every worker do `import init, { SMA, RSI
} from "../../bindings/wasm/pkg/wickra_wasm.js"` without bundler
glue. The inline page module and the worker module both syntax-check
cleanly under `node --check`.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// Module worker for `examples/wasm/parallel_assets.html`. Each worker loads
|
||||
// its own copy of the WebAssembly module, then processes the slice of the
|
||||
// (assets, bars) panel its parent dispatches via `postMessage` and sends
|
||||
// the per-asset "last non-null indicator value" back.
|
||||
|
||||
import init, { SMA, RSI } from "../../bindings/wasm/pkg/wickra_wasm.js";
|
||||
|
||||
const ready = init();
|
||||
|
||||
self.addEventListener("message", async (event) => {
|
||||
await ready;
|
||||
const { panelSlice, indicator } = event.data;
|
||||
|
||||
const results = new Array(panelSlice.length);
|
||||
for (let i = 0; i < panelSlice.length; i++) {
|
||||
const prices = panelSlice[i];
|
||||
const ind = indicator === "sma" ? new SMA(14) : new RSI(14);
|
||||
let last = null;
|
||||
for (let j = 0; j < prices.length; j++) {
|
||||
const v = ind.update(prices[j]);
|
||||
if (v !== null) last = v;
|
||||
}
|
||||
results[i] = last;
|
||||
}
|
||||
|
||||
self.postMessage(results);
|
||||
});
|
||||
Reference in New Issue
Block a user