Skip to content

lib: lazily initialize kEvents and kHandlers maps - #63702

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
araujogui:event-target-lazy-init
Jun 23, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
araujogui:event-target-lazy-init

Conversation

@araujogui

Copy link
Copy Markdown
Member

Defer allocation of kEvents and kHandlers until first use instead of creating them unconditionally in initEventTarget. This avoids two Map allocations for every EventTarget-derived object that never receives a listener, which is the common case for AbortSignal.

Benchmarks
                                         confidence improvement accuracy (*)    (**)   (***)
events/eventtarget-creation.js n=1000000        ***    682.61 %      ±39.42% ±53.12% ±70.51%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 1 comparisons, you can thus
expect the following amount of false-positive results:
  0.05 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.01 false positives, when considering a   1% risk acceptance (**, ***),
  0.00 false positives, when considering a 0.1% risk acceptance (***)
                                                                                   confidence improvement accuracy (*)   (**)   (***)
abort_controller/abort-controller-creation.js kind='AbortController' n=1000000            ***    111.79 %       ±6.06% ±8.16% ±10.83%
abort_controller/abort-controller-creation.js kind='AbortSignal.timeout' n=1000000        ***     21.17 %       ±2.91% ±3.88%  ±5.07%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 2 comparisons, you can thus
expect the following amount of false-positive results:
  0.10 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.02 false positives, when considering a   1% risk acceptance (**, ***),
  0.00 false positives, when considering a 0.1% risk acceptance (***)

Copilot AI review requested due to automatic review settings June 1, 2026 20:17
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added events Issues and PRs related to EventEmitter and the events module. needs-ci PRs that need a full CI run. labels Jun 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR lazily initializes EventTarget’s internal listener/handler maps to reduce upfront allocation costs, and adds a benchmark to measure AbortController/AbortSignal creation overhead.

Changes:

  • Lazily initialize kEvents/kHandlers (undefined until first use) and add early returns/optional chaining in common EventTarget paths.
  • Update lib/events.js helpers to tolerate lazily-initialized EventTargets.
  • Add a benchmark covering AbortController().signal and AbortSignal.timeout() creation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
lib/internal/event_target.js Switch internal maps to lazy init; adjust EventTarget/NodeEventTarget methods to handle undefined maps.
lib/events.js Make getEventListeners() / listenerCount() resilient to missing kEvents map via optional chaining.
benchmark/abort_controller/abort-controller-creation.js Add benchmark for AbortController signal creation and AbortSignal timeout creation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/internal/event_target.js Resolved
Comment thread lib/internal/event_target.js Resolved
@araujogui
araujogui force-pushed the event-target-lazy-init branch from e810426 to 0b5feb1 Compare June 1, 2026 20:40
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
@araujogui
araujogui force-pushed the event-target-lazy-init branch from 0b5feb1 to 49fa612 Compare June 1, 2026 20:47
@codecov

codecov Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.34%. Comparing base (5f96180) to head (49fa612).
⚠️ Report is 213 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63702      +/-   ##
==========================================
+ Coverage   90.30%   90.34%   +0.04%     
==========================================
  Files         730      732       +2     
  Lines      235565   236713    +1148     
  Branches    44421    44604     +183     
==========================================
+ Hits       212718   213859    +1141     
+ Misses      14646    14555      -91     
- Partials     8201     8299      +98     
Files with missing lines Coverage Δ
lib/events.js 99.59% <100.00%> (ø)
lib/internal/event_target.js 99.18% <100.00%> (+0.01%) ⬆️

... and 115 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gurgunday gurgunday added the needs-benchmark-ci PRs that need a benchmark CI run. label Jun 2, 2026
@avivkeller avivkeller added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jun 17, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jun 17, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@avivkeller avivkeller added the commit-queue PRs queued for automated landing through the Commit Queue. label Jun 23, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Jun 23, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 7e31299 into nodejs:main Jun 23, 2026
76 of 77 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 7e31299

richardlau pushed a commit that referenced this pull request Jul 3, 2026
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
PR-URL: #63702
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
aduh95 pushed a commit that referenced this pull request Jul 8, 2026
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
PR-URL: #63702
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Jul 9, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [node](https://nodejs.org) ([source](https://github.com/nodejs/node)) | minor | `26.3.1` → `26.5.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>nodejs/node (node)</summary>

### [`v26.5.0`](https://github.com/nodejs/node/releases/tag/v26.5.0): 2026-07-08, Version 26.5.0 (Current), @&#8203;richardlau

[Compare Source](nodejs/node@v26.3.1...v26.5.0)

##### Notable Changes

##### New release key

Welcome to our newest releaser, [Stewart X Addison](https://github.com/sxa). Future Node.js releases may be signed with his [release key](https://github.com/nodejs/node/blob/main/README.md#release-keys), `655F3B5C1FB3FA8D1A0CA6BDE4A7D232B936D2FD`.

##### Other notable changes

- \[[`55f48446c7`](nodejs/node@55f48446c7)] - **(SEMVER-MINOR)** **buffer**: implement blob.textStream() (Matthew Aitken) [#&#8203;64036](nodejs/node#64036)
- \[[`b373202efc`](nodejs/node@b373202efc)] - **(SEMVER-MINOR)** **esm**: add `--experimental-import-text` flag (Efe) [#&#8203;62300](nodejs/node#62300)
- \[[`39e0c14455`](nodejs/node@39e0c14455)] - **(SEMVER-MINOR)** **perf\_hooks**: sample delay per event loop iteration (Pablo Erhard) [#&#8203;62935](nodejs/node#62935)
- \[[`999a83c937`](nodejs/node@999a83c937)] - **(SEMVER-MINOR)** **stream**: expose ReadableStreamTee (Matteo Collina) [#&#8203;64195](nodejs/node#64195)
- \[[`4e0236dc3d`](nodejs/node@4e0236dc3d)] - **(SEMVER-MINOR)** **tls**: report negotiated TLS groups (Filip Skokan) [#&#8203;64119](nodejs/node#64119)

##### Commits

- \[[`87648c0a6c`](nodejs/node@87648c0a6c)] - **benchmark**: trim down the argon2 sets (Filip Skokan) [#&#8203;64218](nodejs/node#64218)
- \[[`a483bfd3f0`](nodejs/node@a483bfd3f0)] - **buffer**: remove unreachable overflow check in atob (haramjeong) [#&#8203;60161](nodejs/node#60161)
- \[[`6d14279688`](nodejs/node@6d14279688)] - **buffer**: add fast api for isUtf8 and isAscii (Gürgün Dayıoğlu) [#&#8203;64169](nodejs/node#64169)
- \[[`55f48446c7`](nodejs/node@55f48446c7)] - **(SEMVER-MINOR)** **buffer**: implement blob.textStream() (Matthew Aitken) [#&#8203;64036](nodejs/node#64036)
- \[[`a67d9a7a44`](nodejs/node@a67d9a7a44)] - **build**: allow linting node.1 (Aviv Keller) [#&#8203;64157](nodejs/node#64157)
- \[[`06c1fbc25b`](nodejs/node@06c1fbc25b)] - **build**: enable Maglev for riscv64 (Jamie Magee) [#&#8203;62605](nodejs/node#62605)
- \[[`518309c363`](nodejs/node@518309c363)] - **build**: suppress clang errors building libffi on Windows (René) [#&#8203;64222](nodejs/node#64222)
- \[[`6a80ab485c`](nodejs/node@6a80ab485c)] - **build**: add manually-dispatched stress-test workflow (Joyee Cheung) [#&#8203;64118](nodejs/node#64118)
- \[[`f4e7bf1f1c`](nodejs/node@f4e7bf1f1c)] - **build**: pin envinfo versions in github actions (Joyee Cheung) [#&#8203;64117](nodejs/node#64117)
- \[[`66f6ac0d86`](nodejs/node@66f6ac0d86)] - **build**: support setting an emulator from configure script (Ivan Trubach) [#&#8203;53899](nodejs/node#53899)
- \[[`7f26c54aa6`](nodejs/node@7f26c54aa6)] - **child\_process**: fix permission model propagation via NODE\_OPTIONS (Matteo Collina) [#&#8203;63972](nodejs/node#63972)
- \[[`32bb554f5b`](nodejs/node@32bb554f5b)] - **crypto**: fix large DH generator validation (Tobias Nießen) [#&#8203;64092](nodejs/node#64092)
- \[[`0908d76ef6`](nodejs/node@0908d76ef6)] - **crypto**: reject small-order EdDSA points during verify (Filip Skokan) [#&#8203;64026](nodejs/node#64026)
- \[[`7f7e5863c2`](nodejs/node@7f7e5863c2)] - **deps**: update undici to 8.7.0 (Node.js GitHub Bot) [#&#8203;64282](nodejs/node#64282)
- \[[`af91029801`](nodejs/node@af91029801)] - **deps**: update nghttp3 to 1.17.0 (Node.js GitHub Bot) [#&#8203;64182](nodejs/node#64182)
- \[[`2e500ba7b0`](nodejs/node@2e500ba7b0)] - **deps**: update googletest to [`8b53336`](nodejs/node@8b53336) (Node.js GitHub Bot) [#&#8203;64181](nodejs/node#64181)
- \[[`74e3aa24ba`](nodejs/node@74e3aa24ba)] - **deps**: update sqlite to 3.53.3 (Node.js GitHub Bot) [#&#8203;64180](nodejs/node#64180)
- \[[`c7e57f55a7`](nodejs/node@c7e57f55a7)] - **deps**: c-ares: cherry-pick [`8ba37af`](nodejs/node@8ba37af8e3fb) (René) [#&#8203;64110](nodejs/node#64110)
- \[[`879fdc4daf`](nodejs/node@879fdc4daf)] - **deps**: V8: backport [`da20a19`](nodejs/node@da20a197a7f9) (Kevin Gibbons) [#&#8203;64101](nodejs/node#64101)
- \[[`a640543a7c`](nodejs/node@a640543a7c)] - **deps**: V8: cherry-pick [`0cc9eb2`](nodejs/node@0cc9eb22c0b0) (Kevin Gibbons) [#&#8203;64101](nodejs/node#64101)
- \[[`feefd179e5`](nodejs/node@feefd179e5)] - **deps**: V8: cherry-pick [`1a391f9`](nodejs/node@1a391f98cc7a) (Kevin Gibbons) [#&#8203;64101](nodejs/node#64101)
- \[[`8ef643d4b0`](nodejs/node@8ef643d4b0)] - **deps**: update googletest to [`0b1e895`](nodejs/node@0b1e895) (Node.js GitHub Bot) [#&#8203;64039](nodejs/node#64039)
- \[[`9e50bb0655`](nodejs/node@9e50bb0655)] - **dgram**: skip dns.lookup() for literal IP addresses (Ruben Bridgewater) [#&#8203;64133](nodejs/node#64133)
- \[[`dc052c095c`](nodejs/node@dc052c095c)] - **diagnostics\_channel**: return original thenable (Stephen Belanger) [#&#8203;62407](nodejs/node#62407)
- \[[`a22a840293`](nodejs/node@a22a840293)] - **doc**: clarify QUIC stream state wording (EduardF1) [#&#8203;63660](nodejs/node#63660)
- \[[`8d4bec2d71`](nodejs/node@8d4bec2d71)] - **doc**: update Http2SecureServer.on("timeout") default value (YuSheng Chen) [#&#8203;64187](nodejs/node#64187)
- \[[`da88f70afa`](nodejs/node@da88f70afa)] - **doc**: add note on visibility of CI failures to new contributor guide (Stewart X Addison) [#&#8203;64256](nodejs/node#64256)
- \[[`20ce359ccb`](nodejs/node@20ce359ccb)] - **doc**: clarify HTTP/1.1 response ordering (Matteo Collina) [#&#8203;64213](nodejs/node#64213)
- \[[`05eae2835c`](nodejs/node@05eae2835c)] - **doc**: recommend node-stress-single-test for flaky tests (Trivikram Kamat) [#&#8203;64223](