Skip to main content

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. 1

    Time a block of work

    easy10 min

    Measure how long a loop takes and confirm it is not negative.

  2. 2

    Move work out of a loop

    easy10 min

    Compute a value once outside the loop instead of every time.

  3. 3

    Compare two equivalent approaches

    easy10 min

    Show filter+map and a manual loop give the same result.

  4. 4

    Choose the faster membership check

    easy10 min

    Use a Set for a membership check instead of an array.

  5. 5

    Keep code simple by default

    medium10 min

    Show a simple reduce-based sum gives the correct total.

Find the bug(5)

  1. 1

    Fix the work repeated every iteration

    easy8 min

    The unchanging prefix should be computed once, not inside the loop.

  2. 2

    Fix includes used where has would scale better

    easy8 min

    An array membership check is slower than a Set for repeated lookups.

  3. 3

    Fix the elapsed time measured backwards

    easy8 min

    The start time must be subtracted from the end time, not the other way round.

  4. 4

    Fix the rewritten code that changed the answer

    easy8 min

    An optimisation attempt must still give the same result.

  5. 5

    Fix the total never reset between runs

    easy8 min

    Reusing 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