Start with a function you already have.

kernel.js kernel.wasm kernel.js jz as is fast path fallback, tests
  1. Install

    npm install jz
  2. Compile a function

    import jz from 'jz'
    
    const { exports } = jz`
      export const dist = (x, y) =>
        (x*x + y*y) ** 0.5
    `
    
    console.log(exports.dist(3, 4)) // 5
  3. Pass whole arrays

    import jz from 'jz'
    
    const { exports, memory } = jz`
      export const sum = a => {
        let total = 0
        for (const x of a) total += x
        return total
      }
    `
    
    const values = new Float64Array([1, 2, 3])
    console.log(exports.sum(values)) // 6
    memory.reset()
    ValueCrosses
    numberdirectly
    string, array, typed arraycopied
    plain objectby reference
    memory.Float64Array(n)shared, no copy
    heap allocationsfreed on return or by memory.reset()
  4. Ship the .wasm

    npx jz kernel.js               # → kernel.wasm
    npx jz kernel.js -O3           # fastest; -Os smallest
    npx jz kernel.js --host wasi   # standalone WASI module
    npx jz kernel.js --why         # what kept a loop scalar
    import { instantiate } from 'jz/interop' // 19 kB gzip
    
    const wasm = await fetch('kernel.wasm').then(r => r.arrayBuffer())
    const { exports } = instantiate(wasm)
  5. Check both

    Testsrun them against kernel.js and kernel.wasm
    Speedtime the whole call, transfer included
    Scalar loops--why names the operation that blocked SIMD

What compiles

SupportedNot supported
classes, closures, generators, async/awaiteval, Function, with
destructuring, spread, 64-bit BigIntProxy, Reflect, prototype chains
typed arrays, Map, Set, JSON, RegExp, Datetop-level await, computed import()
timers, Web codecs, ES modulesDOM, Node modules, Intl, Temporal

No GC, ASCII case, UTC dates: where it differs from JS.