-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Closed
Labels
v8 engineIssues and PRs related to the V8 dependency.Issues and PRs related to the V8 dependency.
Description
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
Metadata
Metadata
Assignees
Labels
v8 engineIssues and PRs related to the V8 dependency.Issues and PRs related to the V8 dependency.