Try it locally
Multiply two matrices and solve a linear system with HazelJS Numeric, math.js, and ml-matrix. The examples use small inputs so you can inspect every result. Use Node.js 20 or later and an npm registry that serves HazelJS 2.2.0.
mkdir numeric-example
cd numeric-example
npm init -y
npm install @hazeljs/numeric@2.2.0 mathjs@14.9.1 ml-matrix@6.15.1
Save this as compare.mjs.
import { array, matmul, solve } from '@hazeljs/numeric';
import { lusolve, multiply } from 'mathjs';
import { Matrix, solve as solveMatrix } from 'ml-matrix';
const a = [[4, 2], [2, 3]];
const b = [[1, 2], [3, 4]];
const rhs = [6, 5];
const hazelA = array(a);
const mlA = new Matrix(a);
console.log('Matrix product A × B');
console.log('HazelJS Numeric', matmul(hazelA, array(b)).tolist());
console.log('math.js', multiply(a, b));
console.log('ml-matrix', mlA.mmul(new Matrix(b)).to2DArray());
console.log('Solution to A × x = rhs');
console.log('HazelJS Numeric', solve(hazelA, array(rhs)).tolist());
console.log('math.js', lusolve(a, rhs).map(([value]) => value));
console.log('ml-matrix', solveMatrix(mlA, Matrix.columnVector(rhs)).to1DArray());
node compare.mjs
Expect a product of [[10, 16], [11, 16]] and a solution of [1, 1], up to floating-point rounding. Change rhs to [8, 11] to try a solve with expected solution [0.25, 3.5].
Benchmark results
On an Apple M3 Max (arm64, Node.js 23.10.0), HazelJS Numeric multiplied two 512 × 512 matrices in 73.0 ms, about 29% less time than ml-matrix and 81% less than math.js in the same test. Numeric also recorded the lowest median for the dot product and linear solve. Measured results JSON
| Workload | Size | HazelJS Numeric (ms) | math.js (ms) | ml-matrix (ms) |
|---|---|---|---|---|
| Vector addition | 512 | 0.084 | 0.080 | — |
| Dot product | 512 | 0.020 | 0.088 | — |
| Matrix multiplication | 512 × 512 | 72.967 | 386.890 | 102.758 |
| Linear solve | 128 × 128 | 1.034 | 8.510 | 1.071 |
| Reduced SVD | 48 × 48 | 2.931 | — | 1.597 |
Bold shows the lowest measured median in each row. A dash marks a library that the harness did not time for that workload. The solve medians for Numeric and ml-matrix were close on this run.
How the benchmark works
The harness supplies the same float64 values to each library and times repeated calls after imports. The matrix multiplication case prepares each library's inputs in advance. The math.js vector cases include conversion from TypedArrays to JavaScript arrays. The harness checks the benchmark outputs, including the solve against a known solution and the SVD against its expected singular values and reconstruction.
Reproduce this benchmark
Run from a HazelJS source checkout with Node.js 20 or later. The CLI argument sets the main matrix order. The solve and SVD cap their orders separately.
npm ci
npm run build -w @hazeljs/numeric
cd benchmarks/numeric
npm ci
npm start -- 512
The run writes medians and runtime information to benchmarks/numeric/results/latest.json. Numeric package guide
FAQ
- Where does Numeric lead in this benchmark?
- Numeric had the lowest median in the dot product, 512 × 512 matrix multiplication, and 128 × 128 linear solve. These results reflect the tested sizes and runtime.
- Which library was quickest for SVD?
- ml-matrix had the lower median for the verified 48 × 48 SVD, at 1.597 ms versus 2.931 ms for Numeric.