Console.time and Console.timeEnd
You'll learn how to quickly measure how long a block of code takes to run using built-in browser timing methods.
What Are Console.time and Console.timeEnd?
These are two simple console methods that work together like a stopwatch. You call console.time('label') to start the timer, run your code, then call console.timeEnd('label') to stop it and see the elapsed time. The label is just a string identifier so you can run multiple timers simultaneously.
Think of it like timing a race: you click the stopwatch when the runner starts (console.time), let them run, then click it again when they cross the finish line (console.timeEnd). The console automatically displays the duration.
Basic Usage
console.time('array-processing');
const numbers = Array.from({ length: 100000 }, (_, i) => i);
const doubled = numbers.map(n => n * 2);
console.timeEnd('array-processing');
// Output: "array-processing: 8.234ms"
The label must match exactly between the start and end calls. If they don't match, console.timeEnd won't know which timer to stop.
Why Use This Instead of Manual Timing?
You could use Date.now() or performance.now() manually, but console.time is faster to write when you just need a quick measurement during development. It's perfect for comparing two approaches:
console.time('for-loop');
let sum1 = 0;
for (let i = 0; i < 1000000; i++) sum1 += i;
console.timeEnd('for-loop');
console.time('reduce');
const sum2 = Array.from({ length: 1000000 }, (_, i) => i)
.reduce((acc, n) => acc + n, 0);
console.timeEnd('reduce');
This instantly shows you which implementation is faster in your specific environment.
Key Takeaway: Use console.time and console.timeEnd with matching labels to quickly measure code execution time during development—it's the simplest way to spot performance bottlenecks before reaching for more sophisticated profiling tools.