JavaScript Benchmark
Benchmark and compare two JavaScript functions side by side in a web worker.
Input
Output
Guide
What is a JavaScript benchmark?
A JavaScript benchmark measures how fast a piece of code runs by calling it over and over for a fixed time and counting how many operations per second it completes. It's the usual way to compare two approaches to the same problem, for example whether Array.includes() is faster than Array.indexOf() for your data. Results vary between browsers and devices, since each JavaScript engine (V8, SpiderMonkey, JavaScriptCore) optimizes code in its own way.
Tool description
Paste two JavaScript snippets into Function 1 and Function 2 and press Run Benchmark to time them against each other in your browser. Code in the Setup field runs before each snippet, so it's the place for test data. Test Duration sets how long each snippet runs. The result says which function is faster and by how many percent, and shows the operations per second and the average time per call for both, with a bar for each. A difference under 1% counts as a tie. Stop cancels a run that takes too long.
Features
- Runs the benchmark in a Web Worker, so slow code never freezes the page
- A Setup field for test data that isn't timed
- Operations per second, time per call and the percentage difference side by side
How it works
Each snippet becomes its own function, so a return ends just that call. The tool calls it in batches that take at least 1 ms, so reading the clock costs next to nothing, runs a 100 ms warmup so the JIT compiler can optimize the code, and then counts calls for the chosen duration (2 seconds by default). Ops/sec is count / elapsed_ms * 1000. Function 1 runs first, then Function 2. The code runs inside a Web Worker, so it can't reach the page or the DOM, but it has the standard JavaScript globals such as Math, Date and performance.
Tips
- Build test data in the Setup field so it isn't timed, and avoid side effects that pile up between calls.
- Use a longer duration, 3 to 5 seconds, for steadier results on very fast operations.
- Results hold for your browser and machine, so benchmark where the code will really run.