How can you use a JavaScript time function to measure how long a function takes to execute in milliseconds?

I need to determine the execution time of a function with millisecond precision. Back in 2008, the common approach was:

const start = new Date().getTime(); // function execution const end = new Date().getTime(); console.log(Time taken: ${end - start}ms); However, with improvements in the language, performance.now() has become the preferred JavaScript time function for more accurate timing:

const start = performance.now(); // function execution const end = performance.now(); console.log(Time taken: ${end - start}ms); Is this still considered the best method today, or are there newer, more precise ways to measure function execution time in JavaScript?

1 Like

I’ve been working with performance tuning in JS for over a decade now…”* If you’re measuring how long a function takes, performance.now() is still the most accurate JavaScript time function for browser environments. It offers sub-millisecond precision, unlike Date.now() or new Date().getTime() which are only accurate to the millisecond. Here’s a quick snippet:

const start = performance.now();
someFunction();
const end = performance.now();
console.log(`Execution time: ${(end - start).toFixed(3)}ms`);

Still rock-solid in 2025. Unless you’re digging into deeper performance profiling or working in Node.js, this does the job beautifully.

Jumping in as someone who’s spent the last few years optimizing Node.js apps…

Absolutely agree with @tim-khorev performance.now() is your go-to JavaScript time function in the browser. But if you’re working in Node.js, you’ll want to use performance.now() from the perf_hooks module or even process.hrtime() for higher precision:

const { performance } = require('perf_hooks');

const start = performance.now();
someFunction();
const end = performance.now();

console.log(`Took ${(end - start).toFixed(3)}ms`);

It gives you the same high-resolution timing on the backend. Just steer clear of Date.now() for any serious timing—you’ll miss out on that fine-grain accuracy.

Having maintained a few legacy systems over the years, I’ll add this…

Yes to everything above! But just a heads-up for folks supporting older browsers or legacy platforms—Date.now() or new Date().getTime() will still technically work as a JavaScript time function. They’re just far less precise.

If you’re benchmarking something time-sensitive like UI animations or rendering, performance.now() is the smarter choice. It’s monotonic too, so it won’t jump around if the system clock changes—a small detail that matters more often than you’d think.