Console.time() and Performance Timing
You'll learn how to measure how long your code takes to run using built-in console timers.
What is Console Timing?
When you need a quick performance check—how long does this loop take? Is this function slow?—JavaScript's console provides simple timing methods. console.time() starts a named timer, and console.timeEnd() stops it and logs the elapsed time. Think of it like using a stopwatch: you click start before the race, click stop when it's done, and see the duration.
How It Works
You pass a label (a string name) to both methods so they know which timer to track:
console.time('dataProcessing');
// Some code you want to measure
let sum = 0;
for (let i = 0; i < 1000000; i++) {
sum += i;
}
console.timeEnd('dataProcessing');
// Logs something like: "dataProcessing: 3.2ms"
The label acts as an identifier—you can run multiple timers simultaneously with different labels.
Multiple Timers
You can nest or run parallel timers to compare different sections:
console.time('total');
console.time('step1');
// First operation
console.timeEnd('step1');
console.time('step2');
// Second operation
console.timeEnd('step2');
console.timeEnd('total');
This helps you pinpoint which part of your code is the bottleneck.
When to Use It
console.time() is perfect for quick, informal performance checks during development. It's not meant for production monitoring or precise benchmarking (browser overhead varies), but it's invaluable for spotting slow code sections while debugging.
Key Takeaway: Use console.time(label) and console.timeEnd(label) to quickly measure execution time of code blocks during development—perfect for identifying performance bottlenecks with minimal setup.