Course contentsShow
← JavaScript
Lesson 1920 of 2,72340. PerformanceFree lesson

Performance.now for High-Resolution Timing

Measuring code execution with microsecond precision using the Performance API's now() method.

Performance.now for High-Resolution Timing

What you'll learn: How to measure code execution time with microsecond precision using performance.now().

Why performance.now() Exists

You've already used console.time() and console.timeEnd() for basic timing measurements. But when you need higher precision and more control over your timing data, performance.now() is the tool to reach for. It returns a high-resolution timestamp measured in milliseconds with decimal precision (often to microseconds), making it ideal for performance-critical measurements.

How It Works

performance.now() returns the number of milliseconds since the page started loading (in browsers) or since the Node.js process started. Unlike Date.now(), which gives whole milliseconds and can be affected by system clock adjustments, performance.now() is:

  • Monotonic: always increases, never jumps backward
  • High-resolution: provides fractional milliseconds (microsecond precision)
  • Isolated from system time changes
const start = performance.now();

// Code you want to measure
for (let i = 0; i < 1000000; i++) {
  Math.sqrt(i);
}

const end = performance.now();
const duration = end - start;

console.log(`Execution took ${duration.toFixed(3)} ms`);
// Output: Execution took 3.847 ms

Notice those decimal places—you're measuring fractions of a millisecond, which matters when profiling tight loops or comparing micro-optimizations.

When to Use It

Use performance.now() when you need to:

  • Store timing data for later analysis
  • Compare multiple approaches with precision
  • Build custom profiling tools
  • Measure very fast operations (< 10ms)

Key Takeaway: performance.now() gives you microsecond-precision timestamps, perfect for detailed performance measurements where console.time() isn't granular enough or when you need programmatic access to timing data.