Blyx

Finding the nearest center in one pass

Suppose a sensor reports a point at (2, 3). Which reference point is closer, (1, 1) or (5, 7)? In Blyx you can now answer that question without writing an indexing loop.

fn main() {
    let point = [2.0, 3.0];
    println!(squared_distance(point, [1.0, 1.0]));
    println!(squared_distance(point, [5.0, 7.0]));
}

The program prints 5 and 25. The first center is closer. Taking square roots would preserve the order, so a nearest-center classifier can compare the squared values directly.

Run it and change the inputs

Where the loop spent its time

An equivalent loop reads two coordinates with at, subtracts them and accumulates their squared difference. The interpreter dispatches each of those expressions for every coordinate. In 0.2.3 the common one- and two-argument vector calls keep their arguments on the stack. Existing loops pick up that change when they run with the new interpreter.

The new built-in moves the whole reduction into the runtime. It reads each vector once, uses a single accumulator and creates no intermediate difference vector. The CLI interpreter, native backend and WebAssembly runtime implement the same operation. Two empty vectors return zero. Different lengths raise an error. With large enough coordinates the squared result can exceed the range of a double.

What we measured

On a Windows x64 laptop with an Intel Core i5-12450H, we compared a pair of 256-coordinate vectors 64 times per program in Node.js 24.17.0. Each run produced the same total of 298,368. We rotated execution order, discarded three warm-ups per case and took the median of 15 samples. Each sample included creating a WebAssembly instance, parsing the source and running it. Module compilation took place before timing.

Median elapsed time per program
RuntimeProgramTime
0.2.2Indexing loop17.62 ms
0.2.3Same indexing loop16.18 ms
0.2.3squared_distance0.42 ms

The built-in took about one forty-second of the old loop’s time on this input. Native programs already compile loops to machine code, so these interpreter timings cannot predict their speed. Small vectors and programs that spend their time in input or output will have different results.

A smaller download

Removing symbol information from the shipped WebAssembly binary reduced its size from 274,145 to 244,137 bytes. Browser and editor users receive the same runtime. These figures count the uncompressed module and exclude the rest of the extension.

Reproduce the comparison

From a checkout of the release, save the previous interpreter using Git and run the benchmark with Node.js. Use a binary-safe shell for the redirection, such as Bash.

git show v0.2.2:website/public/runtime/blyx.wasm > previous.wasm
node tools/bench-vector-distance.mjs previous.wasm website/public/runtime/blyx.wasm

Download all 45 timings · Benchmark source · Download the CLI · Install the editor release