A CLI to benchmark JavaScript (and TypeScript) on the spot, using JSDoc comments
Insert a JSDoc comment with a @jsbm
tag above a block,
statement or function
example.ts
const original = new Array<number>(2048).fill(0);
/**
* Place the tag before a block or statement
* @jsbm map
*/
{
const arr = original.map((_, i) => i);
}
/**
* Place the tag before a function declaration,
* or arrow function assignment
* @jsbm push
*/
const push = () => {
const arr: number[] = [];
for (let i = 0; i < original.length; i++) {
arr.push(i);
}
return arr;
};
/**
* Pass parameters to the function
* @jsbm prealloc(original)
* @jsbm prealloc(new Array(256).fill(0)) prealloc-256
* @jsbm prealloc(new Array(65536).fill(0)) prealloc-65536
*/
function prealloc(from: number[]) {
const arr = new Array<number>(from.length);
for (let i = 0; i < from.length; i++) {
arr[i] = i;
}
return arr;
}
$ jsbm example.ts
>example.ts [email protected] iter:1 sample:1000
map | 12.97μs ±2.89μs :7%
push | 16.61μs ±7.08μs :1%
prealloc | 4.30μs ±1.88μs :4%
prealloc-256 | 1.35μs ±0.91μs :4%
prealloc-65536 | 128.00μs ±5.21μs :16%
npm install -g @9elt/jsbm
jsbm [runtime...] file... [options]
runtimes:
bun, deno, node
options:
--version, -V print version
--help, -h print help
--keep keep generated file(s)
--code print measured code
--md print results as markdown
--sample number of samples (default: 1000)
--iter measure the code over a number of iterations (default: 1)