HomeBlogDeno 2 Benchmark Api

Deno 2 Benchmark API

Published Dec 15, 2024
Updated Dec 15, 2024
2 minutes read

Deno 2 introduces significant performance improvements and benchmarking capabilities for API development. The new benchmarking API allows developers to measure the performance of their applications and identify bottlenecks in their code. This post provides a brief overview of the Deno 2 benchmark API and how it can be used to optimize code performance.

Benchmarking in Deno 2

The new benchmarking API can be used by the deno bench command to run benchmarks on Deno scripts. The API provides a simple and intuitive way to define and run benchmarks in Deno applications. Here's an example of how the benchmarking API can be used to measure the performance of a function:

// deno_url.ts
 
Deno.bench("url parsing", () => {
  for(let i = 0; i < 1000; i++) {
    new URL("https://example.com/foo/bar?baz=qux");
  }
});

On the terminal, you can run the benchmark using the deno bench command:

deno bench deno_url.ts

The output will display the results of the benchmark, including the number of iterations, average time per iteration, and total time taken to run the benchmark.

Deno 2 Benchmark Output

Let's break down the output:

The URL parsing benchmark is running on Deno 2.1.4 on an Apple M1 Pro processor. The table shows the name of the benchmark (url parsing) with other details, like:

  1. time/iter (avg): 421.6 μs - This is the average time taken per iteration of the URL parsing operation. Each iteration takes about 421.6 microseconds.
  2. iter/s: 2,372 - This shows how many iterations can be performed per second. The benchmark can parse URLs 2,372 times per second.
  3. (min ... max): (401.5 μs ... 734.0 μs) - This shows the range of times:
    • The minimum time taken for an iteration is 401.5 microseconds.
    • The maximum time taken for an iteration is 734.0 microseconds.
  4. Percentile measurements:
    • p75: 426.0 μs - 75% of operations completed within 426.0 microseconds.
    • p99%: 563.8 μs - 99% of operations completed within 563.8 microseconds
    • p995%: 611.3 μs - 99.5% of operations completed within 611.3 microseconds.

Looking at the output, the result suggests that the URL parsing is quite consistent, with most operations completing around 421-426 microseconds, though there are some occasional slower runs up to 734 microseconds.

A more complex example

For a few more complex examples, we can use the benchContext object to set up and tear down the benchmark. Here's an example of a more complex benchmark that measures the performance of a function that sorts an array of numbers:

// file_reading.ts
Deno.bench("File reading", async (b) => {
  // Open a file that we will act upon.
  using file = await Deno.open("test_file.txt", { read: true});
 
  // Tell the benchmarking tool that this is the only section you want
  // to measure.
  b.start();
 
  // Now let's measure how long it takes to read all of the data from the file.
  await new Response(file.readable).arrayBuffer();
 
  // End measurement here.
  b.end();
 
  // Since we opened the file using `using`, it will be closed automatically.
});

Conclusion

The Deno 2 benchmark API provides a powerful tool for measuring the performance of Deno applications. By running benchmarks on your code, you can identify performance bottlenecks and optimize your applications for better performance. The benchmarking API is easy to use and provides detailed information about the performance of your code, making it an essential tool for developers working with Deno. If you are using Node.js, you can also use similar tools like tinybench, mitata.

Resources: