← Back to index
function Deno.bench

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 "https://deno.land/std/testing/asserts.ts";

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");
  }
});

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 "https://deno.land/std/testing/asserts.ts";

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>

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 "https://deno.land/std/testing/asserts.ts";

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>

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 "https://deno.land/std/testing/asserts.ts";

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>

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 "https://deno.land/std/testing/asserts.ts";

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>

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 "https://deno.land/std/testing/asserts.ts";

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>