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
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!