Measure how long a loop takes and confirm it is not negative.
Acceptance criteria
- 1.#out should show true.
JavaScript Tutorial · JavaScript Performance
These 10 tasks go with the JavaScript Performance Basics 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: “Fast code starts with measuring, not guessing where the slow part is.” .
1.Time a block of workexercise · easy · 10 min
Measure how long a loop takes and confirm it is not negative.
Passes when: #out should show true.
2.Move work out of a loopexercise · easy · 10 min · practice plan
Compute a value once outside the loop instead of every time.
Passes when: #out should show PREFIX-a,PREFIX-b.
3.Compare two equivalent approachesexercise · easy · 10 min · practice plan
Show filter+map and a manual loop give the same result.
Passes when: #out should show same result.
4.Choose the faster membership checkexercise · easy · 10 min · practice plan
Use a Set for a membership check instead of an array.
Passes when: #out should show true.
5.Keep code simple by defaultexercise · medium · 10 min · practice plan
Show a simple reduce-based sum gives the correct total.
Passes when: #out should show 10.
6.Fix the work repeated every iterationbug fix · easy · 8 min
The unchanging prefix should be computed once, not inside the loop.
Passes when: #out should show 1.
7.Fix includes used where has would scale betterbug fix · easy · 8 min · practice plan
An array membership check is slower than a Set for repeated lookups.
Passes when: #out should show true.
8.Fix the elapsed time measured backwardsbug fix · easy · 8 min · practice plan
The start time must be subtracted from the end time, not the other way round.
Passes when: #out should show 500 ms elapsed.
9.Fix the rewritten code that changed the answerbug fix · easy · 8 min · practice plan
An optimisation attempt must still give the same result.
Passes when: #out should show same result.
10.Fix the total never reset between runsbug fix · easy · 8 min · practice plan
Reusing the same total across two separate measurements corrupts the second.
Passes when: #out should show 10.
Between them these tasks cover Measuring, Loop cost and Data structure choice.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.