A simple command-line tool for javascript performance testing, inspired by Golang benchmarking.
# install
$ npm install -g @knockkkjs/jsben
# test loop.js
$ jsben loop.js
loop.js:
function BenchmarkLoop() {
for (let i = 0; i < 100000000; i++) {}
}
"jsben" will perform performance testing on every function in the file that starts with "benchmark" (case-insensitive).
output:
os: darwin
node version: v16.16.0
BenchmarkLoop 20 32.6ms/op
The BenchmarkLoop
function was executed a total of 20 times, with an average duration of 32.6 milliseconds per run.
// test.js
function BenchmarkExample() {
for (let i = 0; i < 100000000; i++) {}
}
$ jsben test.js
os: darwin
node version: v16.16.0
BenchmarkExample 20 32.4ms/op
"jsben" also supports asynchronous functions.
// test.js
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
async function BenchmarkAsyncExample() {
await sleep(100);
}
$ jsben test.js
os: darwin
node version: v16.16.0
BenchmarkAsyncExample 9 101ms/op