Measuring Performance

Measure First

Never optimise by guessing. Measure, find the slowest part, improve it, and measure again.

Example

const data = Array.from({ length: 200_000 }, (_, i) => i);

let t = performance.now();
let sum1 = 0;
for (let i = 0; i < data.length; i++) sum1 += data[i];
const loopMs = performance.now() - t;

t = performance.now();
const sum2 = data.reduce((a, b) => a + b, 0);
const reduceMs = performance.now() - t;

console.log("same result:", sum1 === sum2);
console.log("both finish in a few ms:", loopMs < 100 && reduceMs < 100);

Output

same result: true
both finish in a few ms: true

Tools

  • Chrome DevTools Performance tab: record and see long tasks, layout and paint.
  • Lighthouse: page-level scores and suggestions.
  • console.time("label") / console.timeEnd("label") for quick timings.
  • Core Web Vitals in production: LCP (loading), INP (responsiveness), CLS (layout stability).