Learn JavaScript

Lesson 4 of 9 · Performance, Memory, Debugging and the Interview Output Puzzles

Module 17 · Performance, Memory, Debugging and the Interview Output Puzzles

What Each Operation Costs, and When to Optimise at All

ProReading

Rafi runs a small online shop. His order page was fine with 200 orders. With 20000, it freezes for seconds. Nothing in his code looks slow: there is no loop inside a loop, only a few tidy lines with filter and indexOf. Yet one of those tidy lines hides a second loop, and that is the whole problem.

Checking what this lesson needs for you…

What is inside What Each Operation Costs, and When to Optimise at All

  1. Big-O in one page
  2. Array index and length
  3. push and pop
  4. shift and unshift
  5. splice in the middle
  6. includes, indexOf and find against Set.has
  7. Map.get against an object key
  8. Strings: +=, slice and indexOf
  9. sort
  10. The nested-loop trap
  11. All the costs on one page
  12. When to optimise, and when not