calculate execution times of async function over multiple calls

console time nodejs
javascript time since page load
react native performance now
javascript microseconds
import performance javascript
javascript performance library
javascript performance timer

If the do work function was performing some operation, say, picking an item from queue and performing some operation. How would I get the execution times for doWork function over time. I want to find out how much time doWork takes to complete at an average.

Sample Code

function doWork () {
  return Promise.resolve({first: 'Tony', last: 'Starks'})

async function wrapper () {
  const response = await doWork()
  return response

]).then((result) =>


wrapper: 0.388ms
[ { first: 'Tony', last: 'Starks' },
  { first: 'Tony', last: 'Starks' },
  { first: 'Tony', last: 'Starks' } ]
(node:2749) Warning: No such label 'wrapper' for console.timeEnd()
(node:2749) Warning: No such label 'wrapper' for console.timeEnd()

If you're hard-set on testing them in parallel, I recommend this approach:

function doWork () {
  return Promise.resolve({ first: 'Tony', last: 'Stank' })

async function wrapper (index) {
  const response = await doWork()
  return response

  Array(3) // some big number
    .map((wrapper, index) => wrapper(index))
).then((results) =>

How to run async JavaScript functions in sequence or parallel, The async and await keywords are a great addition to Javascript. I'm going to assume you're familiar with Promises and making a simple async/await call. Where it can get tricky is when we have a whole bunch of tasks in an array. We'​ll create one function that will delay for some time, then tell us how  The most frequent kind of PowerShell function is a function that executes code and, when finished, returns control back to the console. This is called synchronous code. This means further code execution will not continue until that function's execution is complete. There are those times when the code inside of your function may take a while, and you don't feel like waiting around until it's

If you're just trying to get your console.time() and console.end() calls to work around your async function, you can generate a unique label each time you call console.time() and console.end() so that the measurement will still work when there are multiple calls in flight at the same time (since each call will then be using its own label):

let wrapperCntr = 0;

async function wrapper () {
  let cnt = wrapperCntr++;
  console.time('wrapper' + cnt);
  const response = await doWork();
  console.timeEnd('wrapper' + cnt);
  return response;

6 points you need to know about async & await in , August 17, 2019 Time to read 6 min read There are many a times where we have a bunch of tasks to be executed sequentially. Or calling multiple APIs in a sequence where one call is dependenat on another. Warning: If you try to invoke an async function using await inside a normal function, you will get a syntax  Each time the original function call is repeated, it clears previous diagnostic records. Executing Connection Operations Asynchronously. Before ODBC 3.8, asynchronous execution was allowed for statement-related operations such as prepare, execute, and fetch, as well as for catalog metadata operations.

Related Question

Why not use the built-in performance package, which is available in node and the browser by default.

The following will work for multiple calls too...

async function timePromise(promiseFunction) {
  try {
    const begin =;
    await promiseFunction();
    const end =;
    const timeTaken_ms = end - begin;
    return timeTaken_ms;
  } catch (error) {

Deeply Understanding JavaScript Async and Await with Examples, Due to JavScript's asynchronous nature, a callback is required in many A callback is nothing special but a function that is executed at some later time. This is affectionately called as Callback Hell or Pyramid of Doom due Looping over multiple promises in a sequence is challenging and non-intuitive. Asynchronous functions are declared with the async keyword (line 5). On line 7, I declare an async operation using the await keyword. The function will pause at the await keyword until the async call completes. Async functions return a promise, so you still use promises to handle the response, as you can see on line 1 above.

Executing Code in Parallel, In this article, we are going to cover running code in parallel using JavaScript and how to measure execution time. If you're not familiar with async/await, promises​, or Promise.all please check out the In some cases, the backend has to handle making multiple requests to the database or external APIs. Simply taking a callback doesn't make a function asynchronous. There are many examples of functions that take a function argument but are not asynchronous. For example there's forEach in Array. It iterates over each item and calls the function once per item. This can be used among other things to calculate total value from a property of each item.

JavaScript Promises and Async/Await: As Fast As Possible™, Let's take a simple example of calling three functions in series. on to the main call stack one at the time where they are executed This is literally a hell because if we had more than 3 function calls, the code could get messier pretty quickly. Async/Await is a fancier syntax to handle multiple promises in  The start() function calls draw() to start the spinner spinning and display it in the UI, hides the Start button so you can't mess up the game by starting it multiple times concurrently, and runs a setTimeout() call that runs a setEndgame() function after a random interval between 5 and 10 seconds has passed.

C++17 STL Cookbook, In order to organize this little choreography, a variable is needed from which the other threads can determine if they must Another nice detail is: If it happens, that the thread which is selected to execute the function in call_once fails because that does multiple things at the same time using asynchronous function calls. Now our total execution time is only as slow as the slowest method, rather than being the cumulative time for all methods executing one after each other. Wrapping up. I hope this post has helped shine a little light on how to use the async/await keywords and how to use Task.WhenAll to run independent methods in parallel.

  • It might be prudent to run the tests of your function end-to-end, otherwise each invocation will be "fighting" all the others for limited CPU time. It would make more sense to do performance testing one at a time, and stress testing the way you're doing now.
  • You are basically doing console.time('wrapper') thrice, then doing console.timeEnd('wrapper') thrice. Make the measurement of the parallel executing tasks by manually subtracting timestamps.
  • possible duplicate of How to measure the execution time of a promise?, unless your question specifically is why the console.time approach didn't work (please edit to make that clearer).
  • @PatrickRoberts Not sure I understand what you are trying to say.
  • @Bergi The code I posted is an over simplification of the actual code. I have built an offline process that performs a bunch of tasks. We are not sure how long each of the task takes. Ideally each of these tasks can be an AWS Lambda but without knowing how long it is taking we dont want to go the lambda way.
  • Updated question with your suggestion.
  • @Perpetualcoder - Why would you update your question with my answer? That's not how things are supposed to work here. You can do several things with my answer such as upvote it, downvote it, comment on it, accept it, but copying it into your question is not one of them. Your question should stay what it originally was (with any needed clarification). Questions are questions, answers are answers and the two should not be merged here on stack overflow. Questions are NOT meant to be a running commentary on your latest thinking, incorporating what you've learned from answers.
  • Ok. Removed the updates. There was no reason to downvote. I had already upvoted it. However, it was not producing the results quite the way I wanted. But your solution got over the initial problem of the warnings thrown for labels going out of context.
  • @Perpetualcoder - What is the "results the way you wanted"? I had a hard time figuring out what you're really asking for beyond this.
  • I would like to know how much time (assuming the function was called 3 times), each call took. This method isolates each call because of the wrapperCntr but.. first call takes 0.6ms then it takes 2.6ms etc. I dont think this is the right behavior.