Skip to content

refactor: use pre-created Set for modifier key lookup in isModifierKey#188

Merged
avinashbot merged 2 commits into
cloudscape-design:mainfrom
TrevorBurnham:optimize-keycode-modifier-set
Jul 10, 2026
Merged

refactor: use pre-created Set for modifier key lookup in isModifierKey#188
avinashbot merged 2 commits into
cloudscape-design:mainfrom
TrevorBurnham:optimize-keycode-modifier-set

Conversation

@TrevorBurnham

Copy link
Copy Markdown
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

codecov Bot commented Jan 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.01%. Comparing base (78240a2) to head (92a6979).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #188   +/-   ##
=======================================
  Coverage   99.00%   99.01%           
=======================================
  Files          44       44           
  Lines        1313     1314    +1     
  Branches      363      341   -22     
=======================================
+ Hits         1300     1301    +1     
- Misses         12       13    +1     
+ Partials        1        0    -1     

☔ View full report in Codecov by Harness.
📢 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.

@avinashbot avinashbot added this pull request to the merge queue Jul 10, 2026
Merged via the queue into cloudscape-design:main with commit 556e877 Jul 10, 2026
113 of 115 checks passed
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.

3 participants