Performance: deno bench for benchmarking
This affects how quickly and accurately you can measure the performance of your code, helping optimize runtime speed and resource use.
import { bench, runBenchmarks } from "https://deno.land/std@0.203.0/testing/bench.ts"; bench({ name: "doSomething benchmark", fn() { doSomething(); }, }); if (import.meta.main) { await runBenchmarks(); }
const start = Date.now(); for(let i=0; i<1000000; i++) { doSomething(); } const end = Date.now(); console.log(`Time: ${end - start}ms`);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual Date.now() timing | 0 | 0 | 0 | [X] Bad |
| deno bench API usage | 0 | 0 | 0 | [OK] Good |
deno bench in Deno?deno bench doesdeno bench runs benchmark tests to measure execution time of code snippets.bench with an object containing name and fn properties.bench({ name: "test", fn: () => { } }). Others are invalid syntax.deno bench?
bench({
name: "sum test",
fn() {
let sum = 0;
for (let i = 0; i < 1000; i++) {
sum += i;
}
}
});deno bench doesbench({
name: "test",
fn: () => {
let x = 0
for (let i = 0; i < 10; i++) {
x += i;
}
}
});let x = 0 before the for loop since for cannot continue the previous statement.fn. The code is synchronous, so no await is needed. The code is syntactically valid and runs fine.for [OK]for loops [OK]await is needed for synchronous loopsbench calls are neededbench mixes timings and does not isolate performance.bench calls each with a unique name and function -> Option BPass the test and this pattern moves unknown