Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Promises and performance #56368

Open
gridagribnoy opened this issue Dec 26, 2024 · 0 comments
Open

Bug in Promises and performance #56368

gridagribnoy opened this issue Dec 26, 2024 · 0 comments

Comments

@gridagribnoy
Copy link

gridagribnoy commented Dec 26, 2024

Version

v22.11.0

Platform

Microsoft Windows NT 6.3.9600.0
x64

Subsystem

No response

What steps will reproduce the bug?

const runTask = async fn => {
  await fn();
};
const iters = 3e6;

async function runWithAwait() {
  let counter = 0;
  const start = Date.now();
  for (let index = 0; index < iters; index++) {
    await "any value :D or task";
    runTask(async () => counter++);
  }
  return { counter, time: (Date.now() - start) / 1000 };
}

async function runWithAwaitWithThen() {
  let counter = 0;
  const start = Date.now();
  for (let index = 0; index < iters; index++) {
    await "any value :D";
    runTask(async () => {}).then(() => counter++);
  }
  return { counter, time: (Date.now() - start) / 1000 };
}

async function run() {
  let counter = 0;
  const start = Date.now();
  for (let index = 0; index < iters; index++) {
    runTask(async () => counter++);
  }
  return { counter, time: (Date.now() - start) / 1000 };
}

async function runWithThen() {
  let counter = 0;
  const start = Date.now();
  for (let index = 0; index < iters; index++) {
    runTask(async () => {}).then(() => counter++);
  }
  return { counter, time: (Date.now() - start) / 1000 };
}

await runWithAwait().then(res => console.log("runWithAwait", res));

await runWithAwaitWithThen().then(res =>
  console.log("runWithAwaitWithThen", res)
);

await run().then(res => console.log("run", res));

await runWithThen().then(res => console.log("runWithThen", res));

result:

runWithAwait { counter: 3000000, time: 1.325 }
runWithAwaitWithThen { counter: 2999998, time: 1.614 }
run { counter: 3000000, time: 8.356 }
runWithThen { counter: 0, time: 10.951 }

How often does it reproduce? Is there a required condition?

Always if not awaited promise

What is the expected behavior? Why is that the expected behavior?

For example, you need to run asynchronous tasks with side effects that you don't need to wait for. Or the results go to the eventmeter, this can be useful for queues.

import EventEmitter from "node:events";

const events = new EventEmitter();

const add = async task => {
  try {
    events.emit("success", await task());
  } catch (err) {
    events.emit("err", { err, task });
  }
};

function addTasks() {
  for (let index = 0; index < 3e6; index++) {
    //if you don't await it, you get bad performance
    add(async () => { 
      const isEven = index % 2 == 0;
      if (isEven) throw "even";
      return index;
    });
  }
}

addTasks();


/* for performance hack */

// async function addTasksHack() {
//   for (let index = 0; index < 3e6; index++) {
//     await "";
//     add(async () => {
//       return index;
//     });
//   }
// }
// addTasksHack();

What do you see instead?

Not needed awaited result, with good performance

Additional information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant