Skip to content

setImmediate function does not work as expected #28016

@wegrzynkiewicz

Description

@wegrzynkiewicz

Version: Deno 2.1.9

I encountered an issue with setImmediate in Deno, which does not seem to work as expected.

import { setImmediate } from "node:timers";

let ticks = 0;
const startTime = performance.now();
const loop = () => {
  ticks++
  if (performance.now() - startTime > 1000) {
    console.log(ticks, "ticks per second");
    return;
  }
  setImmediate(loop);
}
loop();

The code above produces the following results on my machine:

deno 764 ticks per second
node 619805 ticks per second
bun  1093140 ticks per second

When replacing setImmediate(loop) with setTimeout(loop, 0) or setTimeout(loop, 1), the results are:

deno 767 ticks per second
node 786 ticks per second
bun  997 ticks per second

It is extremely strange that the setImmediate function starts so few tasks per second

I will also add that I have tried many different methods:
queueMicrotask unfortunately blocks event loops
setInterval behaves similarly to setTimeout.

Expectations & questions:

  1. I would expect Deno not to have to wait ~1ms before executing the next micro/macro task.
  2. Is there any way to achieve performance similar to Node.js in Deno?
  3. How can I efficiently delegate asynchronous tasks without blocking the event loop and without such a long wait time?

Context:

I'm building a game server that performs hundreds of small synchronous operations around 1000 updates per tick.
A naive synchronous loop that updates each entity blocks the event loop for user interactions.
In one second, I update the game world's physics 30 times, which means approximately 30,000 small updates per second.
When performed synchronously, the performance is sufficient. However, when trying to avoid blocking the event loop, 764 ticks per second is too low.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions