How to measure execution time in JavaScript

Measuring execution time is essential for optimizing code and identifying performance bottlenecks in your JavaScript applications. As the creator of CoreUI with over 25 years of development experience, I’ve measured and optimized thousands of functions across various projects. The most straightforward and accurate solution is to use console.time() for quick measurements or performance.now() for high-precision timing. Both methods are built into modern browsers and provide reliable results.

Use console.time() and console.timeEnd() to measure execution time quickly.

console.time('myOperation')

// Code to measure
let sum = 0
for (let i = 0; i < 1000000; i++) {
  sum += i
}

console.timeEnd('myOperation')

The console.time() method starts a timer with a given label, and console.timeEnd() stops the timer and logs the elapsed time in milliseconds. For more precise measurements, you can use performance.now() which returns a high-resolution timestamp. This method is particularly useful when you need to calculate durations programmatically rather than just logging them to the console.

Best Practice Note

This is the same timing approach we use in CoreUI to benchmark component rendering and interaction performance. For production code, avoid leaving timing calls in place as they add overhead. Use performance.now() when you need microsecond precision or when building performance monitoring tools for real-world usage analytics.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to loop through a 2D array in JavaScript
How to loop through a 2D array in JavaScript

How to change opacity on hover in CSS
How to change opacity on hover in CSS

How to loop inside React JSX
How to loop inside React JSX

How to check if an element is visible in JavaScript
How to check if an element is visible in JavaScript

Answers by CoreUI Core Team