Skip to content

Perf: Use Set for O(1) source origin lookup - #3372

Draft
TomA-R wants to merge 1 commit into
masterfrom
fix/iframe-event-listener-performance
Draft

Perf: Use Set for O(1) source origin lookup#3372
TomA-R wants to merge 1 commit into
masterfrom
fix/iframe-event-listener-performance

Conversation

@TomA-R

@TomA-R TomA-R commented Aug 24, 2026

Copy link
Copy Markdown
Member

Summary

Improves performance of iframe event listener origin validation.

Issue

The _sourceOrigins array was checked using indexOf() on every postMessage event.
This is O(n) complexity, even though typically only 2 origins are checked.

Changes

  • Convert _sourceOrigins from array to Set
  • Use Set.has() for O(1) lookup

Performance Impact

  • Constant time lookup instead of linear
  • Significant improvement in high-frequency messaging scenarios

Testing

  • Build passes with NX
  • No breaking changes to API

Note

Low Risk
Internal lookup change only; origin allowlist contents and validation logic stay the same.

Overview
Speeds up postMessage origin checks in IframeEventListener by storing allowed origins in a Set and using has() instead of indexOf() on an array.

Allowed origins are still the parsed source origin and its www variant. Behavior and public API are unchanged.

Reviewed by Cursor Bugbot for commit 6085fc9. Bugbot is set up for automated code reviews on this repo. Configure here.

Convert _sourceOrigins from array to Set to improve performance of message
event validation. The indexOf() check was O(n), now has() is O(1).

This is called on every postMessage event, so the improvement is significant
in high-frequency messaging scenarios.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant