Skip to content

perf: use sets internally for PressedKeys#419

Open
WarningImHack3r wants to merge 2 commits into
svecosystem:mainfrom
WarningImHack3r:pressed-keys-perf
Open

perf: use sets internally for PressedKeys#419
WarningImHack3r wants to merge 2 commits into
svecosystem:mainfrom
WarningImHack3r:pressed-keys-perf

Conversation

@WarningImHack3r
Copy link
Copy Markdown

I just started using PressedKeysin my project, and I looked at the code because I was curious how it worked

As it turns out, I spotted a few performance points that could be improved, so here we are

Basically, I:

  • transformed every array into Sets for O(1) deletions & lookups, as well as builtin uniqueness
  • avoided double array loops within has by lowercasing keys on-the-fly and leveraging Set methods

All tests pass, so I assume there is no regression and it's safe to merge (maybe?)

I'm not sure how we could measure the improvements, but it's fair to say this is faster than arrays

On another note, pnpm suggested adding missing packages to the workspace, so I thought why not pushing that too as we're here

Tell me what you think!

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 4, 2026

🦋 Changeset detected

Latest commit: 7550768

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
runed Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

// but keep other modifier keys that might still be pressed
// This prevents keys from getting "stuck" in the pressed state
this.#pressedKeys = this.#pressedKeys.filter((k) => modifierKeys.includes(k));
this.#pressedKeys = this.#pressedKeys.intersection(modifierKeys);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Set.prototype.intersection is only baseline since 2024/06, tell me if you want me to add a fallback for that

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