Write a timeIt helper and confirm it returns a non-negative duration.
Acceptance criteria
- 1.#out should show true.
JavaScript Tutorial · JavaScript Performance
These 10 tasks go with the JavaScript Performance Measurement chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “performance.now gives a far more precise timestamp than Date.now for measuring code.” .
1.Time a function with a helperexercise · easy · 10 min
Write a timeIt helper and confirm it returns a non-negative duration.
Passes when: #out should show true.
2.Check the type of the measurementexercise · easy · 10 min · practice plan
Report the type of what performance.now returns.
Passes when: #out should show number.
3.Measure twice and compareexercise · easy · 10 min · practice plan
Run two timings and confirm both completed successfully.
Passes when: #out should show true.
4.Use console.time safelyexercise · easy · 10 min · practice plan
Wrap a loop with console.time and confirm it still runs to completion.
Passes when: #out should show 499500.
5.Collect several measurementsexercise · medium · 10 min · practice plan
Time the same block three times and confirm all are valid.
Passes when: #out should show 3.
6.Fix Date.now used where more precision helpsbug fix · easy · 8 min
performance.now is designed specifically for timing code, and reports fractions of a millisecond.
Passes when: #out should show timed with performance.now.
7.Fix the subtraction done backwardsbug fix · easy · 8 min · practice plan
The start time must be subtracted from the end, not the reverse.
Passes when: #out should show 250 ms measured.
8.Fix the function never actually calledbug fix · easy · 8 min · practice plan
The timer wraps a call that never happens, so the loop never runs.
Passes when: #out should show 499500.
9.Fix console.timeEnd given a different labelbug fix · easy · 8 min · practice plan
The label passed to timeEnd must match the one given to time, or the timer is never closed.
Passes when: #out should show timer closed.
10.Fix only one measurement takenbug fix · easy · 8 min · practice plan
A single measurement can be noisy; several give a better picture.
Passes when: #out should show 3.
Between them these tasks cover performance.now, Timing helpers and Repeated measurement.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.