Skip to content
Related Articles
Open in App
Not now

Related Articles

Measure the duration of async operations in Node.js

Improve Article
Save Article
Like Article
  • Last Updated : 22 Oct, 2021
Improve Article
Save Article
Like Article

Asynchronous operation in Node.js is a non-blocking operation which means if we perform an asynchronous operation at a certain point in code then the code after that is executed and does not wait for this asynchronous operation to complete.

If we want to calculate the duration of an asynchronous operation then we can do it easily inside of our async function using the syntax mentioned below.


const calcTime = async () => {
  const start =;
  await someAsyncOperation();
  const end =
  const duration = end - start;

Project Setup:

Step 1: Install Node.js if you haven’t already.

Step 2: Create a folder for your project and cd (change directory) into it. Create a new file named app.js inside that folder.

Project Structure: After following the steps your project structure will look like the following.

In the code example mentioned below, we have simulated an asynchronous operation using setTimeout() method. We perform the addition operation inside an async function that returns a promise. This promise is resolved with the result of addition after 2 seconds. Finally, we calculate and display the time taken to perform the addition operation inside our IIFE (Immediately Invoked Function Expression).


const asyncAdd = async (a, b) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(a + b);
    }, 2000);
(async() => {
  const startTime =;
  const res = await asyncAdd(5, 2);
  const endTime =;
  const timeTaken = endTime - startTime;
  console.log(`Result of addition = ${res}`);
  console.log(`Time taken to perform addition =
          ${timeTaken} milliseconds`);

Step to run the application: We can execute our app.js file using the following command on the command line.

node app.js


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!