Deno 2 Benchmark API
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:
On the terminal, you can run the benchmark using the deno bench
command:
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.
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:
- 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.
- iter/s: 2,372 - This shows how many iterations can be performed per second. The benchmark can parse URLs 2,372 times per second.
- (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.
- 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:
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
.