Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/deno/cli/tsc/dts/lib.deno.ns.d.ts>Deno.bench

A modern runtime for JavaScript and TypeScript.
Latest
function Deno.bench
import { Deno } from "https://deno.land/x/deno@v2.0.4/cli/tsc/dts/lib.deno.ns.d.ts";
const { bench } = Deno;

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

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>

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>

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>

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>

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>