JavaScript

Measuring JavaScript execution time

Advertisements

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.

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`);
JavaScript

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');
JavaScript

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`);
JavaScript

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

To ensure the execution of a function is fast enough, it can be made a part of our unit tests. Many frameworks (Jest, Jasmine, etc.) allow the 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);
});
JavaScript

And those are all the different ways of measuring JavaScript execution time.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

19 hours ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

1 week ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

2 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

4 weeks ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

1 month ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

1 month ago
Advertisements