JavaScript Performance Basics — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Performance Basics 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 block of work
easy10 minMeasure how long a loop takes and confirm it is not negative.
- 2
Move work out of a loop
easy10 minCompute a value once outside the loop instead of every time.
- 3
Compare two equivalent approaches
easy10 minShow filter+map and a manual loop give the same result.
- 4
Choose the faster membership check
easy10 minUse a Set for a membership check instead of an array.
- 5
Keep code simple by default
medium10 minShow a simple reduce-based sum gives the correct total.
Find the bug(5)
- 1
Fix the work repeated every iteration
easy8 minThe unchanging prefix should be computed once, not inside the loop.
- 2
Fix includes used where has would scale better
easy8 minAn array membership check is slower than a Set for repeated lookups.
- 3
Fix the elapsed time measured backwards
easy8 minThe start time must be subtracted from the end time, not the other way round.
- 4
Fix the rewritten code that changed the answer
easy8 minAn optimisation attempt must still give the same result.
- 5
Fix the total never reset between runs
easy8 minReusing the same total across two separate measurements corrupts the second.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace