tinybench is a tiny (~200 lines), minimal Go tool for benchmarking JavaScript code. tinybench is designed for quickly comparing the performance of small code snippets. This tool is not production-ready - for more comprehensive and accurate benchmarking, please use a more robust benchmarking tool.
tinybench uses the Go exec package to run JavaScript code in a Node.js process. Each benchmark segment is run as many times as possible in a 10 second period, and the minimum, maximum, and median execution times are recorded for comparison.
- Go (1.16 or higher)
- Node.js
To install, first clone the repository:
git clone https://github.com/joshkraft/tinybench.git
cd tinybench
Next, build the binary:
go build
Finally, add the binary to your system's path. You can do this by copying the binary to a directory that is already on your path (e.g., /usr/local/bin on macOS or Linux). Here's an example:
cp tinybench /usr/local/bin/
You should now be able to run tinybench from anywhere on your system.
Use the // tinybench start
and // tinybench stop
delimiters in your JavaScript file to define the code snippets you want to benchmark. You can create as many benchmarks as you want by adding more pairs of delimiters. Here is an example of a file that compares two different for loops:
function forLoop(arr) {
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
}
}
function forOfLoop(arr) {
for (const element of arr) {}
}
const arr = new Array(1000000).fill().map(() => Math.random());
// tinybench start
forLoop(arr);
// tinybench stop
// tinybench start
forOfLoop(arr);
// tinybench stop
Run the tinybench command and pass in the filepath:
tinybench path/to/jsFile.js
Feel free to open issues or pull requests with improvements, suggestions, or bug fixes.