Course contentsShow
← JavaScript
Lesson 1484 of 2,72332. JavaScript in the BrowserFree lesson

Console.time and Performance Measurement

Use console timers to measure how long code blocks take to execute for quick performance checks.

Console.time and Performance Measurement

What you'll learn: How to measure execution time of code blocks using console timers for quick performance checks.

What Are Console Timers?

Console timers let you track how long a piece of code takes to run. Think of it like using a stopwatch: you start the timer before the code runs, then stop it afterward to see the elapsed time. This is invaluable when you're trying to identify slow parts of your code or compare different approaches.

Basic Timer Usage

You create a timer with console.time() by giving it a label (a name), then end it with console.timeEnd() using the same label:

console.time('data-processing');

// Some code you want to measure
let sum = 0;
for (let i = 0; i < 1000000; i++) {
  sum += i;
}

console.timeEnd('data-processing');
// Output: data-processing: 5.234ms

The label connects the start and end points. When console.timeEnd() runs, it automatically logs the elapsed time in milliseconds.

Checking Intermediate Times

You can check the current elapsed time without stopping the timer using console.timeLog():

console.time('multi-step');

// Step 1
let result = [];
for (let i = 0; i < 500000; i++) {
  result.push(i * 2);
}
console.timeLog('multi-step'); // Shows time so far

// Step 2
result = result.filter(n => n % 4 === 0);
console.timeEnd('multi-step'); // Shows total time

This helps you pinpoint which step in a multi-part operation is slowest.

Key Takeaway: Use console.time(label) and console.timeEnd(label) to quickly measure code execution time — perfect for spotting performance bottlenecks during development without needing complex profiling tools.