Skip to main content
function Deno.bench
Unstable

Register a bench which will be run when deno bench is used on the command line and the containing module looks like a bench module. fn can be async if required.

import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";

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

Deno.bench({
  name: "example ignored test",
  ignore: Deno.build.os === "windows",
  fn(): void {
    // 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");
  }
});

Register a bench which will be run when deno bench is used on the command line and the containing module looks like a bench module. fn can be async if required.

import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";

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

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

Parameters

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

Register a bench which will be run when deno bench is used on the command line and the containing module looks like a bench module. fn can be async if required. Declared function must have a name.

import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";

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

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

Parameters

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

Register a bench which will be run when deno bench is used on the command line and the containing module looks like a bench module. fn can be async if required.

import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";

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

Deno.bench("My async test description", { permissions: { read: false } }, async (): Promise<void> => {
  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: () => void | Promise<void>

Register a bench which will be run when deno bench is used on the command line and the containing module looks like a bench module. fn can be async if required.

import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";

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

Deno.bench({ name: "My async test description", permissions: { read: false } }, async (): Promise<void> => {
  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: () => void | Promise<void>

Register a bench which will be run when deno bench is used on the command line and the containing module looks like a bench module. fn can be async if required. Declared function must have a name.

import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";

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

Deno.bench({ permissions: { read: false } }, async function myOtherTestName(): Promise<void> {
  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: () => void | Promise<void>