Skip to main content

JavaScript Performance Measurement — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Performance Measurement lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Time a function with a helper

    easy10 min

    Write a timeIt helper and confirm it returns a non-negative duration.

  2. 2

    Check the type of the measurement

    easy10 min

    Report the type of what performance.now returns.

  3. 3

    Measure twice and compare

    easy10 min

    Run two timings and confirm both completed successfully.

  4. 4

    Use console.time safely

    easy10 min

    Wrap a loop with console.time and confirm it still runs to completion.

  5. 5

    Collect several measurements

    medium10 min

    Time the same block three times and confirm all are valid.

Find the bug(5)

  1. 1

    Fix Date.now used where more precision helps

    easy8 min

    performance.now is designed specifically for timing code, and reports fractions of a millisecond.

  2. 2

    Fix the subtraction done backwards

    easy8 min

    The start time must be subtracted from the end, not the reverse.

  3. 3

    Fix the function never actually called

    easy8 min

    The timer wraps a call that never happens, so the loop never runs.

  4. 4

    Fix console.timeEnd given a different label

    easy8 min

    The label passed to timeEnd must match the one given to time, or the timer is never closed.

  5. 5

    Fix only one measurement taken

    easy8 min

    A single measurement can be noisy; several give a better picture.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace