📖 Scenario: You want to measure how fast a function runs in your Deno project. Benchmarking helps you see which code is quicker and find ways to improve it.
🎯 Goal: Build a simple benchmark test using deno bench to measure the speed of a function that sums numbers from 1 to 1000.
📋 What You'll Learn
Create a function called sumToThousand that adds numbers from 1 to 1000
Write a benchmark using deno bench with the name sum benchmark
Call the sumToThousand function inside the benchmark
Export the benchmark so deno bench can run it
💡 Why This Matters
🌍 Real World
Benchmarking helps developers find slow parts in their code and improve app speed.
💼 Career
Knowing how to write benchmarks with deno bench is useful for performance testing in Deno projects.
Progress0 / 4 steps
1
Create the sum function
Create a function called sumToThousand that returns the sum of numbers from 1 to 1000 using a for loop.
Deno
Hint
Use a for loop from 1 to 1000 and add each number to a total variable.
2
Set up the benchmark
Add a deno.bench call with the name sum benchmark and a function parameter called bench.
Deno
Hint
Use deno.bench with a name string and an arrow function.
3
Call the sum function inside the benchmark
Inside the deno bench function, call sumToThousand() to measure its speed.
Deno
Hint
Simply call sumToThousand() inside the arrow function passed to deno.bench.
4
Export the benchmark
Add export before the deno.bench call to make the benchmark runnable by Deno.
Deno
Hint
Use export const before the deno.bench call to export it.
Practice
(1/5)
1. What is the main purpose of deno bench in Deno?
easy
A. To format code automatically
B. To check for syntax errors in code
C. To deploy Deno applications to the cloud
D. To measure how fast a piece of code runs
Solution
Step 1: Understand the role of benchmarking
Benchmarking is used to measure the speed or performance of code.
Step 2: Identify what deno bench does
deno bench runs benchmark tests to measure execution time of code snippets.
Final Answer:
To measure how fast a piece of code runs -> Option D
Quick Check:
Benchmarking = measuring speed [OK]
Hint: Benchmark means measuring speed or performance [OK]
Common Mistakes:
Confusing benchmarking with syntax checking
Thinking it deploys code
Assuming it formats code automatically
2. Which of the following is the correct syntax to define a benchmark in Deno?
Hint: Use bench({ name, fn }) to define benchmarks [OK]
Common Mistakes:
Using assignment instead of function call
Incorrect object syntax
Using invalid keywords like 'benchmark'
3. What will be the output behavior when running this benchmark code with deno bench?
bench({
name: "sum test",
fn() {
let sum = 0;
for (let i = 0; i < 1000; i++) {
sum += i;
}
}
});
medium
A. It prints the time taken to run the loop 1000 times
B. It prints the sum value after the loop
C. It throws a syntax error because of missing return
D. It runs but does not show any output
Solution
Step 1: Understand what deno bench does
It runs the benchmark function multiple times and reports the time taken.
Step 2: Analyze the code behavior
The code sums numbers but does not return or print the sum. The benchmark tool measures execution time, not return values.
Final Answer:
It prints the time taken to run the loop 1000 times -> Option A
Quick Check:
Benchmarks show timing, not return values [OK]
Hint: Benchmarks report time, not return values [OK]
Common Mistakes:
Expecting printed sum output
Thinking a return is required
Assuming no output is shown
4. Identify the error in this benchmark code:
bench({
name: "test",
fn: () => {
let x = 0
for (let i = 0; i < 10; i++) {
x += i;
}
}
});
medium
A. Missing semicolon after let x = 0
B. Using arrow function instead of regular function
C. No error, code runs fine
D. Missing await keyword inside fn
Solution
Step 1: Check syntax carefully
JavaScript allows optional semicolons. Automatic Semicolon Insertion (ASI) inserts a semicolon after let x = 0 before the for loop since for cannot continue the previous statement.
Step 2: Verify other potential issues
Arrow functions are valid for fn. The code is synchronous, so no await is needed. The code is syntactically valid and runs fine.
Final Answer:
No error, code runs fine -> Option C
Quick Check:
ASI handles missing semicolon before for [OK]
Hint: Semicolons are optional in JS; ASI works before for loops [OK]
Common Mistakes:
Thinking missing semicolon is a syntax error
Believing arrow functions are invalid in benchmarks
Assuming await is needed for synchronous loops
5. You want to benchmark two functions: one that sums numbers from 1 to 1000, and another that multiplies numbers from 1 to 1000. How should you write the benchmark code to compare their speeds correctly?
hard
A. Run one function inside bench and call the other outside
B. Define two separate bench calls each with a unique name and function
C. Put both functions inside one bench call and run them sequentially
D. Use bench only for the sum function and measure multiply manually