From 7d98aca796522400e82cb83198973086659bc2ff Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Mon, 26 Jan 2026 14:24:07 -0500 Subject: [PATCH] refactor: use pre-created Set for modifier key lookup in isModifierKey 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. --- src/internal/keycode.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/internal/keycode.ts b/src/internal/keycode.ts index 613dca4..8870569 100644 --- a/src/internal/keycode.ts +++ b/src/internal/keycode.ts @@ -21,8 +21,11 @@ export enum KeyCode { meta = 91, } +// Pre-created Set for O(1) lookup instead of creating a new array on every call +const modifierKeyCodes = new Set([KeyCode.shift, KeyCode.alt, KeyCode.control, KeyCode.meta]); + export function isModifierKey(event: KeyboardEvent) { // we do not want to highlight focused element // when special keys are pressed - return [KeyCode.shift, KeyCode.alt, KeyCode.control, KeyCode.meta].includes(event.keyCode); + return modifierKeyCodes.has(event.keyCode); }