Giter Site home page Giter Site logo

tinybench's Introduction

tinybench

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.

How it works

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.

Requirements

  • Go (1.16 or higher)
  • Node.js

Installation

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.

Usage

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

tinybench demo

Contributing

Feel free to open issues or pull requests with improvements, suggestions, or bug fixes.

tinybench's People

Contributors

joshkraft avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.