Skip to content

Conversation

@TrevorBurnham
Copy link
Contributor

This PR replaces the inline array creation in isModifierKey with a pre-created Set for O(1) lookup, eliminating array allocation on every function call.

Motivation

The isModifierKey function is called on every keydown event via the focus-visible module's handleKeydown handler. The previous implementation created a new array on every call:

return [KeyCode.shift, KeyCode.alt, KeyCode.control, KeyCode.meta].includes(event.keyCode);

This means every keypress triggered:

  1. Array allocation for 4 elements
  2. Linear search via .includes()

This could contribute to typing feeling sluggish.

Changes

src/internal/keycode.ts: Create the Set once at module load time and use Set.has() for O(1) lookup:

const modifierKeyCodes = new Set([KeyCode.shift, KeyCode.alt, KeyCode.control, KeyCode.meta]);

export function isModifierKey(event: KeyboardEvent) {
  return modifierKeyCodes.has(event.keyCode);
}

Benefits

  • No allocation per call (Set created once at module load)
  • O(1) lookup instead of O(n) linear search
  • Cleaner separation of data and logic

Testing

All 17 focus-visible tests pass, including tests for each modifier key (Shift, Alt, Control, Meta).


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Replace inline array creation with a pre-created Set for O(1) lookup.
This eliminates array allocation on every keydown event.

Before:
  return [KeyCode.shift, KeyCode.alt, KeyCode.control, KeyCode.meta].includes(event.keyCode);

After:
  const modifierKeyCodes = new Set([KeyCode.shift, KeyCode.alt, KeyCode.control, KeyCode.meta]);
  return modifierKeyCodes.has(event.keyCode);

This is significant because isModifierKey is called on every keydown event
via the focus-visible module's handleKeydown handler.
@TrevorBurnham TrevorBurnham requested a review from a team as a code owner January 26, 2026 19:56
@TrevorBurnham TrevorBurnham requested review from avinashbot and removed request for a team January 26, 2026 19:56
@avinashbot avinashbot enabled auto-merge January 28, 2026 15:15
@codecov
Copy link

codecov bot commented Jan 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.00%. Comparing base (c09d82c) to head (7d98aca).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #188   +/-   ##
=======================================
  Coverage   99.00%   99.00%           
=======================================
  Files          42       42           
  Lines        1101     1102    +1     
  Branches      280      293   +13     
=======================================
+ Hits         1090     1091    +1     
  Misses         11       11           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

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.

2 participants