Search code examples
javascriptnode.jsasynchronousasync-awaitsetimmediate

nodejs aync function taking more time when I replace function call with another function which is similar


Can someone explain to me why there is less time required when I run below code with:

for2=longTsk()  --  approx 2500ms every time

and for

for2=longTsk3() -- approx 3000ms every time

The plain for loop in both the functions requires approx 1500ms every time

function longTsk() {
  return new Promise((resolve) => {
    setImmediate(() => {
      for (let i = 0; i < 2019000000; i++) {

      }
      console.log('HOILA');
      resolve()
    });
  });
}

function longTsk3() {
  return new Promise((resolve) => {
    setImmediate(() => {
      for (let j = 0; j < 2019000000; j++) {

      }
      console.log('HOILA');
      resolve()
    });
  });
}

const date = Date.now()

async function doForAll() {

  const for1 = longTsk()
  const for2 = longTsk() //when i change this to longtsk3 exact double time is required
  await Promise.all([for1, for2])
  console.log(Date.now() - date);
}

doForAll()
console.log('sldkfgj');
console.log('lskdjglkjdfg');

The first time I ran with longTsk3 and then longTsk in this screen shot

first time i ran with longTsk3 and then longTsk in this screen shot

Why replacing the second function call with another function (longTsk3) which is similar, takes 500 ms more? This time scales may change from machine to machine, but there is significant time difference for sure in two situations when run on a same machine!


Solution

  • There is no difference in code between longTsk3 and longTsk.

    The key here is that the same function is called.
    and when the same function is called, the time cost is reduced.

    The time spent in actual operation can be accurately measured as follows.

    1. If done separately, it takes the same amount of time.
    async function doForAll() {
        const for1 = longTsk()
        const for2 = longTsk3()
        var date = Date.now()
        await Promise.all([for1])
        console.log(Date.now() - date); // 1784
    
    
        date = Date.now()
        await Promise.all([for2])
        console.log(Date.now() - date); // 1789
    } 
    
    1. In the case of longTsk & longTsk, since it has already been executed, the cost seems to be reduced the next time it is called.
    async function doForAll() {
        const for1 = longTsk()
        const for2 = longTsk()
        var date = Date.now()
        await Promise.all([for1])
        console.log(Date.now() - date); // 1789
    
        date = Date.now()
        await Promise.all([for2])
        console.log(Date.now() - date); // 1183
    }
    
    1. Even when calling longTsk3 twice in a row, the exact same result as above could be obtained.
    async function doForAll() {
        const for1 = longTsk3()
        const for2 = longTsk3()
        var date = Date.now()
        await Promise.all([for1])
        console.log(Date.now() - date); // 1784
    
    
        date = Date.now()
        await Promise.all([for2])
        console.log(Date.now() - date); // 1185
    } 
    

    In other words, your problem can be seen that the cost is reduced when the same function is called.