JavaScript Test Suite Performance with JSBenchmark

Photo by Patrick on Unsplash

JS Benchmark

Getting Started

npm i jsbenchmark
import * as JSBench from "jsbenchmark";

Usage

Defining a Test Suite

import * as JSBench from "jsbenchmark";new JSBench.TestSuite({
async: true,
passes: 25,
operations: 1000000,
maxRuntime: 1000,
})
.add("double not operator", () => {
~~Math.PI;
})
.add("or operator", () => {
Math.PI | 0;
})
.add("signed right shift operator", () => {
Math.PI >> 0;
})
.add("parseInt", () => {
parseInt(Math.PI);
})
.add("round", () => {
Math.round(Math.PI);
})
.add("floor", () => {
Math.floor(Math.PI);
})
.add("ceil", () => {
Math.ceil(Math.PI);
})
.add("trunc", () => {
Math.trunc(Math.PI);
})
.add("toFixed", () => {
Math.PI.toFixed();
})
.on("test", (event) => {
const test = event.test;
const resultSet = event.resultSet;
console.log(test.name);
resultSet.log();
})
.on("suite", (event) => {
const result = event.result;
result.log();
})
.run();

Adding tests

Add Test

let obj = {};new JSBench.TestSuite({
async: true,
passes: 25,
operations: 1000000,
maxRuntime: 30000,
})
.add("obj[i] = i", (i) => {
obj[i] = i;
})
let obj = {};new JSBench.TestSuite()
.add("obj.hasOwnProperty(i);", (i) => {
obj.hasOwnProperty(i);
})
.add("i in obj", (i) => {
i in obj;
})
.add("i in obj", (i) => {
return i in obj;
})

Add Manual Test

let n = 0;new JSBench.TestSuite({
async: true,
passes: 25,
operations: 1000000,
maxRuntime: 30000,
})
.addManual(
"++n (manual)",
(operations) => {
for (let i = 0; i < operations; i++) {
++n;
}
},
1000000
)
new JSBench.TestSuite({
operations: 1000000, // <-- Use default of test suite
})
.addManual("name", (operations) => {
for (let i = 0; i < operations; i++) {}
});
new JSBench.TestSuite({
operations: 1000000,
})
.addManual(
"name",
(operations) => {
for (let i = 0; i < operations; i++) {}
},
10000 // <!-- Use parameter
);

Individual Test

import * as JSBench from "jsbenchmark"new JSBench.Test("Log 10", () => {
Math.log10(2)
}, {
operations: 1000000
})
.run()
.log();
total time: 10.31500    ops/second: 96,946,195

Events

.on("pass", (event) => {
const test = event.test;
const result = event.result;
console.log(test.name)
result.log();
})
.on("test", (event) => {
const test = event.test;
const resultSet = event.resultSet;
console.log(test.name)
resultSet.log();
})
.on("suite", (event) => {
const result = event.result;
result.log();
})
.on("complete", () => {
console.log("All tests complete.");
})

Comparing Results

let n = 0;new JSBench.TestSuite({
async: true,
passes: 25,
operations: 1000000,
maxRuntime: 30000,
})
.add("++n", () => {
return ++n;
})
.addManual(
"++n (manual)",
(operations) => {
for (let i = 0; i < operations; i++) {
++n;
}
},
1000000
)
.on("test", (event) => {
const test = event.test;
const resultSet = event.resultSet;
console.log(test.name);
resultSet.log();
})
.on("suite", (event) => {
const result = event.result;
result.log();
})
.run();
++n
avg: 11.28820 ops/second: 90,034,364
++n (manual)
avg: 3.33000 ops/second: 311,779,625

Avant-garde experimental artist — creative professional leveraging technology for immersive experiences

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store