Skip to content

Conversation

@TrevorBurnham
Copy link
Contributor

@TrevorBurnham TrevorBurnham commented Aug 24, 2025

This commit replaces several instances of setTimeout(..., 0) with queueMicrotask to improve performance and align more closely with browser behavior for microtask scheduling.

The main changes are:

  • In MutationObserverListener, mutation observer callbacks are now scheduled with queueMicrotask, as specified by the DOM standard.
  • In Fetch, the creation of a ReadableStream from a buffer now uses queueMicrotask to make the operation asynchronous more efficiently.
  • In BrowserWindow, postMessage now uses queueMicrotask to dispatch the message event.

I've ignored the setTimeout in @packages/happy-dom/src/window/BrowserWindow.ts where it's being used to simulate smooth scrolling, since in that case the macrotask scheduling is necessary to accurately reflect DOM behavior.

Resolves #1880

@TrevorBurnham TrevorBurnham changed the title chore: [#1880] Replace setTimeout with queueMicrotask fix: [#1880] Replace setTimeout with queueMicrotask where appropriate Aug 24, 2025
…appropriate

This commit replaces several instances of `setTimeout(..., 0)` with
`queueMicrotask` to improve performance and align more closely with
browser behavior for microtask scheduling.

The main changes are:

- In `MutationObserverListener`, mutation observer callbacks are now scheduled with `queueMicrotask`, as specified by the DOM standard.
- In `Fetch`, the creation of a `ReadableStream` from a buffer now uses `queueMicrotask` to make the operation asynchronous more efficiently.
- In `BrowserWindow`, `postMessage` now uses `queueMicrotask` to dispatch the `message` event.

I've ignored the `setTimeout` in
`@packages/happy-dom/src/window/BrowserWindow.ts` where it's being used
to simulate smooth scrolling, since in that case the macrotask
scheduling is necessary to accurately reflect DOM behavior.
@TrevorBurnham
Copy link
Contributor Author

@capricorn86 I've updated this branch. All checks are passing.

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

Successfully merging this pull request may close these issues.

Replace setTimeout with queueMicrotask where possible

1 participant