From 6e3190a44a4992cf92d31d9438378d8fb29ce72c Mon Sep 17 00:00:00 2001 From: kingchenc Date: Sat, 23 May 2026 00:45:01 +0200 Subject: [PATCH] examples(wasm): add a browser parallel-assets demo via Web Workers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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`. --- examples/wasm/parallel_assets.html | 190 +++++++++++++++++++++++++++++ examples/wasm/parallel_worker.js | 27 ++++ 2 files changed, 217 insertions(+) create mode 100644 examples/wasm/parallel_assets.html create mode 100644 examples/wasm/parallel_worker.js diff --git a/examples/wasm/parallel_assets.html b/examples/wasm/parallel_assets.html new file mode 100644 index 00000000..4b054c36 --- /dev/null +++ b/examples/wasm/parallel_assets.html @@ -0,0 +1,190 @@ + + + + + Wickra WASM — parallel assets + + + +

Wickra WASM — parallel assets

+

+ Builds a synthetic (assets, bars) panel, runs a serial + baseline on the main thread, then dispatches the same workload to a + pool of Workers — each loading its own copy of the + WebAssembly module — and reports the speedup. The browser counterpart + of examples/node/parallel_assets.js and + examples/rust/src/bin/parallel_assets.rs. +

+ +

+ + + + + +

+ +
+

Serial

+

Parallel

+

Speedup

+
+ +

Loading WASM module…

+
+ + + + diff --git a/examples/wasm/parallel_worker.js b/examples/wasm/parallel_worker.js new file mode 100644 index 00000000..fc367d51 --- /dev/null +++ b/examples/wasm/parallel_worker.js @@ -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); +});