Sunday, 12 December 2021

Is there a Performance Difference of Non-Parallel Async Loop vs Sync Loop in Node.js?

I refactored JavaScript code for Node.js (v10.13.0) which was previously synchronous into asynchronous code using async/await. What I noticed afterwards was a performance degradation of ~3x slower program execution time.

Is there a performance penalty when transforming a chain of synchronous function calls into asynchronous function calls?

Simplified Example

Changing synchronous code

function fn1() {  
   return 1;
}

function fn2() { 
   return fn1();
}

(function() {
  const result = fn2();
});

into asynchronous code:

async function fn1() {  
   return 1;
}

async function fn2() { 
   return await fn1();
}

(async function() {
   const result = await fn2();
})();

Is there any event-loop-magic which could make the latter code slower in a Node.js webapp?



from Is there a Performance Difference of Non-Parallel Async Loop vs Sync Loop in Node.js?

No comments:

Post a Comment