Deno has a built-in benchmark runner that you can use for checking performance of JavaScript or TypeScript code.

Quickstart

Firstly, let’s create a file url_bench.ts and register a bench using the Deno.bench() function.

Secondly, run the benchmark using the deno bench subcommand.

  1. deno bench --unstable url_bench.ts
  2. cpu: Apple M1 Max
  3. runtime: deno 1.21.0 (aarch64-apple-darwin)
  4. file:///dev/deno/url_bench.ts
  5. benchmark time (avg) (min max) p75 p99 p995
  6. --------------------------------------------------- -----------------------------
  7. URL parsing 17.29 µs/iter (16.67 µs 153.62 µs) 17.25 µs 18.92 µs 22.25 µs

To define a benchmark you need to register it with a call to the Deno.bench API. There are multiple overloads of this API to allow for the greatest flexibility and easy switching between the forms (eg. when you need to quickly focus a single bench for debugging, using the only: true option):

  1. // Compact form: name and function
  2. Deno.bench("hello world #1", () => {
  3. new URL("https://deno.land");
  4. });
  5. // Compact form: named function.
  6. Deno.bench(function helloWorld3() {
  7. new URL("https://deno.land");
  8. });
  9. // Longer form: test definition.
  10. Deno.bench({
  11. name: "hello world #2",
  12. fn: () => {
  13. new URL("https://deno.land");
  14. });
  15. // Similar to compact form, with additional configuration as a second argument.
  16. Deno.bench("hello world #4", { permissions: { read: true } }, () => {
  17. new URL("https://deno.land");
  18. });
  19. // Similar to longer form, with test function as a second argument.
  20. Deno.bench(
  21. { name: "hello world #5", permissions: { read: true } },
  22. () => {
  23. new URL("https://deno.land");
  24. },
  25. );
  26. // Similar to longer form, with a named test function as a second argument.
  27. Deno.bench({ permissions: { read: true } }, function helloWorld6() {
  28. new URL("https://deno.land");
  29. });

You can also bench asynchronous code by passing a bench function that returns a promise. For this you can use the async keyword when defining a function:

  1. Deno.bench("async hello world", async () => {
  2. await 1;
  3. });

Grouping and baselines

When registering a bench case, it can be assigned to a group, using Deno.BenchDefinition.group option:

```ts, ignore // url_bench.ts Deno.bench(“url parse”, { group: “url” }, () => { new URL(““); });

  1. $ deno bench --unstable time_bench.ts
  2. cpu: Apple M1 Max
  3. runtime: deno 1.21.0 (aarch64-apple-darwin)
  4. file:///dev/deno/time_bench.ts
  5. benchmark time (avg) (min max) p75 p99 p995
  6. --------------------------------------------------------- -----------------------------
  7. Date.now() 125.24 ns/iter (118.98 ns 559.95 ns) 123.62 ns 150.69 ns 156.63 ns
  8. performance.now() 2.67 µs/iter (2.64 µs 2.82 µs) 2.67 µs 2.82 µs 2.82 µs
  9. summary
  10. 21.29x times faster than performance.now()

To run a benchmark, call deno bench with the file that contains your bench function. You can also omit the file name, in which case all benchmarks in the current directory (recursively) that match the glob {*_,*.,}bench.{ts, tsx, mts, js, mjs, jsx, cjs, cts} will be run. If you pass a directory, all files in the directory that match this glob will be run.

The glob expands to:

  • files named bench.{ts, tsx, mts, js, mjs, jsx, cjs, cts},
  • or files ending with .bench.{ts, tsx, mts, js, mjs, jsx, cjs, cts},
  • or files ending with _bench.{ts, tsx, mts, js, mjs, jsx, cjs, cts}
  1. # Run all benches in the current directory and all sub-directories
  2. # Run all benches in the util directory
  3. deno bench util/
  4. # Run just my_bench.ts
  5. deno bench my_bench.ts

⚠️ If you want to pass additional CLI arguments to the bench files use -- to inform Deno that remaining arguments are scripts arguments.

  1. # Pass additional arguments to the bench file
  2. deno bench my_test.ts -- -e --foo --bar

deno bench uses the same permission model as deno run and therefore will require, for example, --allow-write to write to the file system during benching.

To see all runtime options with deno bench, you can reference the command line help:

Filtering

There are a number of options to filter the benches you are running.

Benches can be run individually or in groups using the command line --filter option.

Assuming the following benches:

  1. Deno.bench({
  2. name: "my-bench",
  3. fn: () => {/* bench function zero */},
  4. });
  5. Deno.bench({
  6. name: "bench-1",
  7. fn: () => {/* bench function one */},
  8. });
  9. Deno.bench({
  10. name: "bench2",
  11. fn: () => {/* bench function two */},
  12. });

This command will run all of these benches because they all contain the word “bench”.

  1. deno bench --filter "bench" benchmarks/

On the flip side, the following command uses a pattern and will run the second and third benchmarks.

  1. deno bench --filter "/bench-*\d/" benchmarks/

To let Deno know that you want to use a pattern, wrap your filter with forward-slashes like the JavaScript syntactic sugar for a regex.

Within the benches themselves, you have two options for filtering.

Filtering out (ignoring these benches)

Sometimes you want to ignore benches based on some sort of condition (for example you only want a benchmark to run on Windows). For this you can use the ignore boolean in the bench definition. If it is set to true the test will be skipped.

Filtering in (only run these benches)

Sometimes you may be in the middle of a performance problem within a large bench class and you would like to focus on just that single bench and ignore the rest for now. For this you can use the only option to tell the benchmark harness to only run benches with this set to true. Multiple benches can set this option. While the benchmark run will report on the success or failure of each bench, the overall benchmark run will always fail if any bench is flagged with only, as this is a temporary measure only which disables nearly all of your benchmarks.

  1. Deno.bench({
  2. name: "Focus on this bench only",
  3. only: true,
  4. fn() {
  5. // bench complicated stuff
  6. });