| 1 | // Benchmark: feel-wasm (one-shot vs compiled) vs feelin (JS engine). |
| 2 | // |
| 3 | // Prerequisites: |
| 4 | // wasm-pack build crates/feel-wasm --target nodejs |
| 5 | // cd crates/feel-wasm && npm install |
| 6 | // Run: |
| 7 | // node bench.mjs (or: npm run bench) |
| 8 | // |
| 9 | // Measures, per workload, the throughput of three strategies: |
| 10 | // 1. feel-wasm one-shot — evaluate(src, ctx): parse + JS<->WASM boundary each call |
| 11 | // 2. feel-wasm compiled — compile(src) once, then prog.evaluate(ctx): parse once |
| 12 | // 3. feelin — evaluate(src, ctx): the JS engine, parses each call |
| 13 | // |
| 14 | // (1) vs (2) shows the parse-once win; (2) vs (3) is the cross-engine comparison. |
| 15 | |
| 16 | import { evaluate as wasmEvaluate, compile as wasmCompile } from "./pkg/feel_wasm.js"; |
| 17 | import { evaluate as feelinEvaluate } from "feelin"; |
| 18 | |
| 19 | // Same three workloads as the feel-core criterion bench, so numbers line up. |
| 20 | // [label, expression, context] |
| 21 | const workloads = [ |
| 22 | ["trivial", "1 + 1", {}], |
| 23 | ["decision", 'if person.age >= 18 then "adult" else "minor"', { person: { age: 21 } }], |
| 24 | ["loop", "for i in [1,2,3,4,5] return i * 2", {}], |
| 25 | ]; |
| 26 | |
| 27 | const ITERATIONS = 50_000; |
| 28 | const WARMUP = 2_000; |
| 29 | |
| 30 | // Time `fn` over `iters` runs after `warmup` warmup runs; return ns per op. |
| 31 | function timeNsPerOp(fn, iters = ITERATIONS, warmup = WARMUP) { |
| 32 | for (let i = 0; i < warmup; i++) fn(); |
| 33 | const start = performance.now(); |
| 34 | for (let i = 0; i < iters; i++) fn(); |
| 35 | const elapsedMs = performance.now() - start; |
| 36 | return (elapsedMs * 1e6) / iters; |
| 37 | } |
| 38 | |
| 39 | const fmt = (ns) => { |
| 40 | const opsPerSec = 1e9 / ns; |
| 41 | return `${ns.toFixed(1).padStart(10)} ns/op ${Math.round(opsPerSec).toLocaleString().padStart(13)} ops/s`; |
| 42 | }; |
| 43 | |
| 44 | // Sanity-check that all three strategies agree before timing. |
| 45 | function assertAgreement(label, src, ctx) { |
| 46 | const a = JSON.stringify(wasmEvaluate(src, ctx)); |
| 47 | const prog = wasmCompile(src); |
| 48 | const b = JSON.stringify(prog.evaluate(ctx)); |
| 49 | prog.free(); |
| 50 | let c; |
| 51 | try { |
| 52 | // feelin's evaluate returns { value, warnings }; compare the value. |
| 53 | c = JSON.stringify(feelinEvaluate(src, ctx).value); |
| 54 | } catch (e) { |
| 55 | c = `<feelin error: ${e.message}>`; |
| 56 | } |
| 57 | const note = a === c ? "" : ` (feelin differs: ${c})`; |
| 58 | if (a !== b) throw new Error(`${label}: feel-wasm one-shot vs compiled disagree (${a} vs ${b})`); |
| 59 | console.log(`# ${label}: "${src}" -> ${a}${note}`); |
| 60 | } |
| 61 | |
| 62 | console.log(`# ${ITERATIONS.toLocaleString()} iterations per measurement, ${WARMUP.toLocaleString()} warmup\n`); |
| 63 | |
| 64 | for (const [label, src, ctx] of workloads) { |
| 65 | assertAgreement(label, src, ctx); |
| 66 | |
| 67 | const oneShot = timeNsPerOp(() => wasmEvaluate(src, ctx)); |
| 68 | |
| 69 | const prog = wasmCompile(src); |
| 70 | const compiled = timeNsPerOp(() => prog.evaluate(ctx)); |
| 71 | prog.free(); |
| 72 | |
| 73 | const feelin = timeNsPerOp(() => feelinEvaluate(src, ctx)); |
| 74 | |
| 75 | console.log(` feel-wasm one-shot ${fmt(oneShot)}`); |
| 76 | console.log(` feel-wasm compiled ${fmt(compiled)}`); |
| 77 | console.log(` feelin ${fmt(feelin)}`); |
| 78 | console.log(` -> compiled is ${(oneShot / compiled).toFixed(1)}x the one-shot path; ` + |
| 79 | `${(feelin / compiled).toFixed(1)}x feelin (compiled), ${(feelin / oneShot).toFixed(2)}x feelin (one-shot)\n`); |
| 80 | } |