The JS runtimes
Published in

The JS runtimes

Benchmark your code with Deno bench

Purpose

Usage

import { bench, runBenchmarks } from "https://deno.land/std/testing/bench.ts";

Step 1 — register or bench

bench(function bench1(b) {
b.start();
//...code to benchmark...
b.stop();
});
BenchmarkDefinition {
func: BenchmarkFunction;
name: string;
runs?: number;
}
bench({func: function bench1(b) {
b.start();
//...code to benchmark...
b.stop();
},
name: 'bench test 1',
runs: 10 });

Step 2 — run benchmarks

BenchmarkRunOptions {
only?: RegExp;
skip?: RegExp;
silent?: boolean;
}
await runBenchmarks();//run with default options, no skip, all execute, output on consoleawait runBenchmarks({only: /bench\d/, skip: /bench/, silent: true});//run only tests matching bench[0-9], skip any that doesn't have number, do not produce output on console
await runBenchmarks({silent: true}, (progress) => {});
BenchmarkRunProgress {
queued?: Array<{ name: string; runsCount: number }>;
running?: { name: string; runsCount: number; measuredRunsMs: number[] };
state?: ProgressState;
}

Step 3 — Output

BenchmarkResult {
name: string;
totalMs: number;
runsCount: number;
measuredRunsAvgMs: number;
measuredRunsMs: number[];
}
BenchmarkRunResult {
filtered: number;
results: BenchmarkResult[];
}

Examples

import { assert } from "https://deno.land/std/testing/asserts.ts"
import { bench, runBenchmarks } from "https://deno.land/std/testing/bench.ts";
import { getRandomValues8, getRandomValues16, getRandomValues32 } from "./deno_some_library.ts";
Deno.test('bench 1000 8,16,32 bit random values',
async () => {
bench(function benchBasic(b) {
b.start();
getRandomValues8(1000);
getRandomValues16(1000);
getRandomValues32(1000);
b.stop();
});
const result=await runBenchmarks();
assert(result.results.length === 1);
assert(result.results[0].measuredRunsMs.length === 1);
});
deno test 
running 1 tests
test benchmarking tests ... running 1 benchmark ...
benchmark benchBasic ...
2ms
benchmark result: DONE. 1 measured; 0 filtered
ok (5ms)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (5ms)--//Result
{
filtered: 0,
results: [
{
name: "benchBasic",
totalMs: 2,
runsCount: 1,
measuredRunsAvgMs: 2,
measuredRunsMs: [ 2 ]
}
]
}
import { assert } from "https://deno.land/std/testing/asserts.ts"
import { bench, runBenchmarks } from "https://deno.land/std/testing/bench.ts";
import { getRandomValues8, getRandomValues16, getRandomValues32 } from "./deno_some_library.ts";
Deno.test('bench 5000 8, 16, 32 bit random values for 10K runs',
async () => {
bench({func: function benchWithOptions(b) {
b.start();
getRandomValues8(5000);
getRandomValues16(5000);
getRandomValues32(5000);
b.stop();
}, name: 'bench 5000 8, 16, 32 bit random values for 10K runs', runs: 10000});
const result=await runBenchmarks({silent: true});
assert(result.results.length === 1);
assert(result.results[0].measuredRunsMs.length === 10000);
});
deno test 
running 1 tests
test benchmarking tests ... ok (657ms)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (658ms)--//Result
{
filtered: 0,
results: [
{
name: "bench 5000 8, 16, 32 bit random values for 10K runs",
totalMs: 602,
runsCount: 10000,
measuredRunsAvgMs: 0.0602,
measuredRunsMs: [
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
... 9900 more items
]
}
]
}
Deno.test('bench fetchData',
async () => {
bench(async function fetchData(b) {
const urls = new Array(50).fill("https://deno.land/");
b.start();
await Promise.all(
urls.map(
async (denoland: string) => {
const r = await fetch(denoland);
await r.text();
},
),
);
b.stop();
});
const result=await runBenchmarks({silent:true});
assert(result.results.length === 1);
assert(result.results[0].measuredRunsMs.length === 1);
});
deno test --allow-net 
running 1 tests
test benchmarking tests ... ok (367ms)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (367ms)--//Result
{
filtered: 0,
results: [
{
name: "fetchData",
totalMs: 360,
runsCount: 1,
measuredRunsAvgMs: 360,
measuredRunsMs: [ 360 ]
}
]
}
import { assert } from "https://deno.land/std/testing/asserts.ts"
import { bench, runBenchmarks } from "https://deno.land/std/testing/bench.ts";
import { getRandomValues8, getRandomValues16, getRandomValues32 } from "./deno_some_library.ts";
const baseline=1300;Deno.test('benchmarking tests',
async () => {
bench({func: function benchWithOptions(b) {
b.start();
getRandomValues8(15000);
getRandomValues16(15000);
getRandomValues32(15000);
b.stop();
}, name: 'bench 15000 8, 16, 32 bit random values for 10K runs', runs: 10000});
const result=await runBenchmarks({silent: true});
const runResult=result.results[0];
assert(result.results.length === 1);
assert(runResult.measuredRunsMs.length === 10000);
assert(runResult.totalMs < (baseline+baseline*.1));
}
);
import { assert } from "https://deno.land/std/testing/asserts.ts"
import { bench, runBenchmarks } from "https://deno.land/std/testing/bench.ts";
import { getRandomValues8, getRandomValues16, getRandomValues32 } from "./deno_some_library.ts";
const baseline=1300;Deno.test('benchmarking tests',
async () => {
bench(function benchBasic(b) {
b.start();
getRandomValues8(1000);
getRandomValues16(1000);
getRandomValues32(1000);
b.stop();
});
bench({func: function benchWithOptions(b) {
b.start();
getRandomValues8(15000);
getRandomValues16(15000);
getRandomValues32(15000);
b.stop();
}, name: 'bench 15000 8, 16, 32 bit random values for 10K runs', runs: 10000});
bench(async function fetchData(b) {
const urls = new Array(50).fill("https://deno.land/");
b.start();
await Promise.all(
urls.map(
async (denoland: string) => {
const r = await fetch(denoland);
await r.text();
},
),
);
b.stop();
});
const result=await runBenchmarks({silent: true});
assert(result.results.length === 3);
}
);
deno test --allow-net 
running 1 tests
test benchmarking tests ... ok (1620ms)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (1621ms)--//Result
{
filtered: 0,
results: [
{
name: "benchBasic",
totalMs: 2,
runsCount: 1,
measuredRunsAvgMs: 2,
measuredRunsMs: [ 2 ]
},
{
name: "bench 15000 8, 16, 32 bit random values for 10K runs",
totalMs: 1244,
runsCount: 10000,
measuredRunsAvgMs: 0.1244,
measuredRunsMs: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
... 9900 more items
]
},
{
name: "fetchData",
totalMs: 306,
runsCount: 1,
measuredRunsAvgMs: 306,
measuredRunsMs: [ 306 ]
}
]
}

--

--

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