JS: Timing, Benchmark Functions

By Xah Lee. Date: . Last updated: .

Benchmark JavaScript Code

install Deno.

use Deno.bench to test speed.

let's say you want to compare the speed of 2 functions ff vs gg.

First, you need to create a file. Let's say bench.js

in the script, write:

// file name: bench.js

// define ff here
const ff = ((x) => x+1);

// define gg here
const gg = ((x) => x+2);

// test speed of two functions, ff and gg
Deno.bench( "test ff", (() => { ff(Math.random())}));
Deno.bench( "test gg", (() => { gg(Math.random())}));

then, in terminal, run this:

deno bench bench.js

result:

js deno bench 2023-10-20 xrMg
js deno bench 2023-10-20 xrMg

JavaScript, Speed Comparison

BUY Ξ£JS JavaScript in Depth