Skip to main content
Deno.bench - Deno documentation
function Deno.bench
bench(b: BenchDefinition): void

Register a benchmark test which will be run when deno bench is used on the command line and the containing module looks like a bench module.

If the test function (fn) returns a promise or is async, the test runner will await resolution to consider the test complete.

import { assertEquals } from "jsr:@std/assert";

Deno.bench({
  name: "example test",
  fn() {
    assertEquals("world", "world");
  },
});

Deno.bench({
  name: "example ignored test",
  ignore: Deno.build.os === "windows",
  fn() {
    // This test is ignored only on Windows machines
  },
});

Deno.bench({
  name: "example async test",
  async fn() {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  }
});

Parameters

Return Type

void
bench(
name: string,
fn: (b: BenchContext) => void | Promise<void>,
): void

Register a benchmark test which will be run when deno bench is used on the command line and the containing module looks like a bench module.

If the test function (fn) returns a promise or is async, the test runner will await resolution to consider the test complete.

import { assertEquals } from "jsr:@std/assert";

Deno.bench("My test description", () => {
  assertEquals("hello", "hello");
});

Deno.bench("My async test description", async () => {
  const decoder = new TextDecoder("utf-8");
  const data = await Deno.readFile("hello_world.txt");
  assertEquals(decoder.decode(data), "Hello world");
});

Parameters

name: string
fn: (b: BenchContext) => void | Promise<void>

Return Type

void
bench(fn: (b: BenchContext) => void | Promise<void>): void

Register a benchmark test which will be run when deno bench is used on the command line and the containing module looks like a bench module.

If the test function (fn) returns a promise or is async, the test runner will await resolution to consider the test complete.

import { assertEquals } from "jsr:@std/assert";

Deno.bench(function myTestName() {
  assertEquals("hello", "hello");
});

Deno.bench(async function myOtherTestName() {
  const decoder = new TextDecoder("utf-8");
  const data = await Deno.readFile("hello_world.txt");
  assertEquals(decoder.decode(data), "Hello world");
});

Parameters

fn: (b: BenchContext) => void | Promise<void>

Return Type

void
bench(
name: string,
options: Omit<BenchDefinition, "fn" | "name">,
fn: (b: BenchContext) => void | Promise<void>,
): void

Register a benchmark test which will be run when deno bench is used on the command line and the containing module looks like a bench module.

If the test function (fn) returns a promise or is async, the test runner will await resolution to consider the test complete.

import { assertEquals } from "jsr:@std/assert";

Deno.bench(
  "My test description",
  { permissions: { read: true } },
  () => {
   assertEquals("hello", "hello");
  }
);

Deno.bench(
  "My async test description",
  { permissions: { read: false } },
  async () => {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  }
);

Parameters

name: string
options: Omit<BenchDefinition, "fn" | "name">
fn: (b: BenchContext) => void | Promise<void>

Return Type

void
bench(
options: Omit<BenchDefinition, "fn">,
fn: (b: BenchContext) => void | Promise<void>,
): void

Register a benchmark test which will be run when deno bench is used on the command line and the containing module looks like a bench module.

If the test function (fn) returns a promise or is async, the test runner will await resolution to consider the test complete.

import { assertEquals } from "jsr:@std/assert";

Deno.bench(
  { name: "My test description", permissions: { read: true } },
  () => {
    assertEquals("hello", "hello");
  }
);

Deno.bench(
  { name: "My async test description", permissions: { read: false } },
  async () => {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  }
);

Parameters

options: Omit<BenchDefinition, "fn">
fn: (b: BenchContext) => void | Promise<void>

Return Type

void
bench(
options: Omit<BenchDefinition, "fn" | "name">,
fn: (b: BenchContext) => void | Promise<void>,
): void

Register a benchmark test which will be run when deno bench is used on the command line and the containing module looks like a bench module.

If the test function (fn) returns a promise or is async, the test runner will await resolution to consider the test complete.

import { assertEquals } from "jsr:@std/assert";

Deno.bench(
  { permissions: { read: true } },
  function myTestName() {
    assertEquals("hello", "hello");
  }
);

Deno.bench(
  { permissions: { read: false } },
  async function myOtherTestName() {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  }
);

Parameters

options: Omit<BenchDefinition, "fn" | "name">
fn: (b: BenchContext) => void | Promise<void>

Return Type

void