How to Measure Performance in JavaScript: Simple Methods
You can measure performance in JavaScript using
performance.now() for high-resolution timestamps or console.time() and console.timeEnd() to time code execution. These methods help you find how long a piece of code takes to run.Syntax
performance.now() returns the current time in milliseconds with microsecond precision, useful for measuring elapsed time.
console.time(label) starts a timer with a name, and console.timeEnd(label) stops it and logs the elapsed time with that label.
javascript
const start = performance.now(); // code to measure const end = performance.now(); console.log(`Elapsed time: ${end - start} ms`); console.time('myTimer'); // code to measure console.timeEnd('myTimer');
Example
This example shows how to measure the time taken to run a loop using both performance.now() and console.time().
javascript
function measureLoop() { const iterations = 1000000; // Using performance.now() const start = performance.now(); let sum = 0; for (let i = 0; i < iterations; i++) { sum += i; } const end = performance.now(); console.log(`performance.now(): Loop took ${end - start} ms`); // Using console.time() console.time('loopTimer'); sum = 0; for (let i = 0; i < iterations; i++) { sum += i; } console.timeEnd('loopTimer'); } measureLoop();
Output
performance.now(): Loop took 15.234 ms
loopTimer: 14.987 ms
Common Pitfalls
- Using
Date.now()for performance measurement can be less precise and affected by system clock changes. - Not labeling timers uniquely when using
console.time()andconsole.timeEnd()can cause confusion or errors. - Measuring very fast code without enough iterations may give misleading results due to timer resolution limits.
javascript
/* Wrong way: Using Date.now() for short code */ const startWrong = Date.now(); // very fast code const endWrong = Date.now(); console.log(`Elapsed time (Date.now): ${endWrong - startWrong} ms`); /* Right way: Using performance.now() */ const startRight = performance.now(); // very fast code const endRight = performance.now(); console.log(`Elapsed time (performance.now): ${endRight - startRight} ms`);
Output
Elapsed time (Date.now): 0 ms
Elapsed time (performance.now): 0.012 ms
Quick Reference
| Method | Description | Use Case |
|---|---|---|
| performance.now() | Returns high-resolution time in milliseconds | Measure precise elapsed time for code execution |
| console.time(label) / console.timeEnd(label) | Starts and stops a timer with a label, logs elapsed time | Quick timing in console for code blocks |
| Date.now() | Returns current timestamp in milliseconds | Not recommended for precise performance measurement |
| Performance API (performance.mark, performance.measure) | Advanced timing with named marks and measures | Detailed profiling and custom performance tracking |
Key Takeaways
Use performance.now() for precise timing of code execution.
console.time() and console.timeEnd() provide easy-to-use timers with labels.
Avoid Date.now() for performance measurement due to low precision.
Run enough iterations to get reliable timing results.
Label timers clearly to avoid confusion in console output.