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
Time a function with a helper
easy10 minWrite a timeIt helper and confirm it returns a non-negative duration.
- 2
Check the type of the measurement
easy10 minReport the type of what performance.now returns.
- 3
Measure twice and compare
easy10 minRun two timings and confirm both completed successfully.
- 4
Use console.time safely
easy10 minWrap a loop with console.time and confirm it still runs to completion.
- 5
Collect several measurements
medium10 minTime the same block three times and confirm all are valid.
Find the bug(5)
- 1
Fix Date.now used where more precision helps
easy8 minperformance.now is designed specifically for timing code, and reports fractions of a millisecond.
- 2
Fix the subtraction done backwards
easy8 minThe start time must be subtracted from the end, not the reverse.
- 3
Fix the function never actually called
easy8 minThe timer wraps a call that never happens, so the loop never runs.
- 4
Fix console.timeEnd given a different label
easy8 minThe label passed to timeEnd must match the one given to time, or the timer is never closed.
- 5
Fix only one measurement taken
easy8 minA 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