Measuring JavaScript execution time

When it comes to performance of applications, measuring performance becomes important. For anything to be optimized, it must be measured first, optimized, and measured again to capture gains. Measuring JavaScript execution time thus becomes one of these steps.

Modern browsers and the Node.js platform provide various API’s to measure code execution time.

- Advertisement -

We will be discussing a few ways of measuring JavaScript execution time in this post.

1. Date object

The easiest way to track execution time is to use a date object. Using Date.now() that returns the total number of milliseconds elapsed since the Unix epoch, we can store the value before and after the execution of the function to be measured and then get the difference of the two.

const start = Date.now();
 
await functionToBeMeasured(); 

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

2. Console time

Another easy solution is to use a console timer. The console.time() method starts a timer with a label. And a subsequent call to the console.timeEnd() method with the same label will output the time elapsed since the method was started.

console.time('Execution Time');
 
await functionToBeMeasured(); 

console.timeEnd('Execution Time');

3. Performance timers

Console timers do not provide high accuracy. If we want accuracy in 1-millisecond increments, we can use high-resolution timers like performance.now(). It also avoids some synchronization issues with the system clock.

const start = performance.now();
 
await functionToBeMeasured(); 

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

Note: If measuring in Node, process.hrtime.bigint() returns accuracy in nanoseconds.

This covers the ways of measuring JavaScript execution time. If we run into a scenario wherein we want to start measuring JavaScript execution time for multiple functions, we can make use of the Performance API and Performance Observer. They provide markers to track multiple entries and measure them independently.

Measuring JavaScript execution time in Unit tests

For ensuring execution of functions is fast enough, it can be made a part of our unit tests. Many frameworks (Jest, Jasmine, etc.) allow setting of a timeout for the execution of a test. The timeout feature can then be used to fail a test if the method takes longer to execute.

With Jasmine:

describe('testing time duration', () => {
  it('does not take longer than 5 seconds', async () => {
    await functionToBeMeasured();
  }, 5000);
});

Recent Articles

How to sort a Set in JavaScript

ES6 introduced the set data structure in JavaScript. But sets are not ordered abstract data structures. So there is no .sort() property...

Debugging CSS scroll using one simple style

I have been doing a lot of complicated front-end work off lately and that always brings me back to the class conundrum...

CSS :has a parent selector now

CSS now includes a :has selector that allows us to apply styles on the basis of what is happening inside an element....

How to fix “invalid active developer path” after MacOS update

If you are here, then you are getting an "invalid active developer path" error on running commands in the terminal after a...

Getting the value of an input element as a number without parseInt

Every once in a while, you come across something and you think, how did I not know this earlier? valueAsNumber is that thing...

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Hi there! Want some more knowledge?

Think that the knowledge shared is helpful? You might want to give our mailing list a try. We'll send you 2-4 emails a month, right when new posts come out.

Hi there! Want some more knowledge?

Think that the knowledge shared is helpful? You might want to give our mailing list a try. We'll send you 2-4 emails a month, right when new posts come out.