Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use binary search for faster array operations #52

Merged
merged 1 commit into from
Jul 2, 2020

Conversation

nolanlawson
Copy link
Contributor

Helps a lot with #44

This implements a binary search for most of the RecordStore operations - get(), add(), delete(). Using the benchmark mentioned in #44, running in Node v14 on Ubuntu on my XPS 13, I measured the following improvement (median of 3 iterations):

  • Before: 30621ms
  • After: 5506ms (5.56x improvement)

There are still further improvements that can be made (mostly in RecordStore.add in the value comparison), but I'm holding off since this PR is already big enough and 5.5x is already a pretty big improvement.

I considered switching to a completely different backing store, but that seemed like it would have been a lot of work. This way we can keep the existing Array backing store and just optimize some of the lookups.

@nolanlawson nolanlawson changed the title fix: use binary search for faster array operations perf: use binary search for faster array operations Jul 2, 2020
@nolanlawson
Copy link
Contributor Author

FWIW the project that piqued my interest in this issue is emoji-picker-element. Just tested loading the full emoji database for that project, and it's dropped from 13.5s to 430ms. Progress! 🙂

@dumbmatter
Copy link
Owner

This is an awesome improvement. It speeds up the test suite by 20% too :)

I'm going to merge this an do a new release soon.

@dumbmatter dumbmatter merged commit 03b6162 into dumbmatter:master Jul 2, 2020
let mid;
while (low < high) {
// tslint:disable-next-line:no-bitwise
mid = (low + high) >>> 1; // like Math.floor((low + high) / 2) but fast
Copy link

Choose a reason for hiding this comment

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

🙏 Thank you for explaining!

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.

None yet

3 participants