Bird
Raised Fist0
Denoframework~20 mins

deno bench for benchmarking - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Deno Benchmark Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Deno benchmark output?
Consider this Deno benchmark code snippet. What will be printed when running deno bench?
Deno
import { bench } from "https://deno.land/std@0.177.0/testing/bench.ts";

bench({
  name: "simple loop",
  fn() {
    let sum = 0;
    for (let i = 0; i < 1000; i++) {
      sum += i;
    }
  },
});
AIt prints the benchmark name and the time taken to run the loop.
BIt prints the sum of numbers from 0 to 999.
CIt throws a runtime error because 'bench' is not a function.
DIt prints nothing because the benchmark function is not awaited.
Attempts:
2 left
💡 Hint
Think about what deno bench does with the bench function.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Deno benchmark with async function?
You want to benchmark an asynchronous function using Deno's bench. Which code snippet is correct?
Abench({ name: "async test", async fn() { await new Promise(r => setTimeout(r, 10)); } });
Bbench({ name: "async test", fn: async () => { await new Promise(r => setTimeout(r, 10)); } });
Cbench({ name: "async test", fn() { await new Promise(r => setTimeout(r, 10)); } });
Dbench({ name: "async test", fn: function() { await new Promise(r => setTimeout(r, 10)); } });
Attempts:
2 left
💡 Hint
Remember how to define async functions in object literals.
🔧 Debug
advanced
2:00remaining
Why does this Deno benchmark not run?
Given this code, why does deno bench not execute the benchmark?
Deno
import { bench } from "https://deno.land/std@0.177.0/testing/bench.ts";

function test() {
  bench({
    name: "test",
    fn() {
      // some code
    },
  });
}

// test() is never called
ABecause the import URL is incorrect and <code>bench</code> is undefined.
BBecause the <code>bench</code> function is inside a function that is never called, so no benchmark is registered.
CBecause <code>bench</code> must be awaited to register benchmarks.
DBecause the <code>fn</code> function is empty and skipped by Deno.
Attempts:
2 left
💡 Hint
Think about when benchmarks get registered.
❓ state_output
advanced
2:00remaining
What is the output of this Deno benchmark with setup and teardown?
Analyze this benchmark code. What will be printed when running deno bench?
Deno
import { bench } from "https://deno.land/std@0.177.0/testing/bench.ts";

let counter = 0;

bench({
  name: "increment counter",
  fn() {
    counter++;
  },
  teardown() {
    console.log(`Counter after benchmark: ${counter}`);
  },
});
AIt throws an error because teardown is not a valid option.
BIt prints only the benchmark timing; the console.log in teardown is ignored.
CIt prints 'Counter after benchmark: 0' because fn is not run.
DIt prints the benchmark timing and then 'Counter after benchmark: 1'.
Attempts:
2 left
💡 Hint
Consider when teardown runs relative to fn.
🧠 Conceptual
expert
2:00remaining
How does Deno's benchmark runner handle multiple benchmarks with the same name?
If you define two benchmarks with the same name property in Deno using bench, what happens when you run deno bench?
ADeno runs only the first benchmark and ignores the second with the same name.
BDeno throws an error and refuses to run benchmarks due to duplicate names.
CDeno runs both benchmarks separately and shows timing for each, despite the name clash.
DDeno merges the two benchmarks into one and averages their timings.
Attempts:
2 left
💡 Hint
Think about how test runners handle duplicate names.

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

  1. Step 1: Understand the role of benchmarking

    Benchmarking is used to measure the speed or performance of code.
  2. Step 2: Identify what deno bench does

    deno bench runs benchmark tests to measure execution time of code snippets.
  3. Final Answer:

    To measure how fast a piece of code runs -> Option D
  4. 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?
easy
A. bench({ name: "test", fn: () => { /* code */ } })
B. bench = function(name, fn) { /* code */ }
C. benchmark test() { /* code */ }
D. bench: { name = "test", fn() { /* code */ } }

Solution

  1. Step 1: Recall the official benchmark syntax

    The correct way is to call bench with an object containing name and fn properties.
  2. Step 2: Compare options to the correct pattern

    bench({ name: "test", fn: () => { /* code */ } }) matches the pattern bench({ name: "test", fn: () => { } }). Others are invalid syntax.
  3. Final Answer:

    bench({ name: "test", fn: () => { /* code */ } }) -> Option A
  4. Quick Check:

    Use object with name and fn keys [OK]
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

  1. Step 1: Understand what deno bench does

    It runs the benchmark function multiple times and reports the time taken.
  2. 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.
  3. Final Answer:

    It prints the time taken to run the loop 1000 times -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. 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

Solution

  1. Step 1: Understand benchmarking multiple functions

    Each benchmark should be defined separately with a unique name to measure independently.
  2. Step 2: Why separate bench calls are needed

    Running both in one bench mixes timings and does not isolate performance.
  3. Final Answer:

    Define two separate bench calls each with a unique name and function -> Option B
  4. Quick Check:

    Separate benchmarks for accurate comparison [OK]
Hint: Use separate bench calls with unique names [OK]
Common Mistakes:
  • Combining multiple tests in one bench
  • Measuring one function outside bench
  • Skipping benchmark for one function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard