perf: use Set for O(1) lookups in uniq (#129)#130
Merged
Conversation
yokuze
requested changes
Mar 25, 2026
Contributor
yokuze
left a comment
There was a problem hiding this comment.
@jthomerson This works as-is, but I offered a couple of alternative implementations that are presumably a little faster and less code, if you'd like.
The perf tests were run in Chrome and since you may care most about Node.js, they could use a sanity check in Node.
Contributor
|
Also, built-in |
Both `standardUniq` and `iterateeUniq` used `indexOf` to check for duplicates, which is O(n) per element — making the overall algorithm O(n²). At 40k elements this takes over a second. `Set.has` uses SameValueZero equality, which matches `indexOf` behavior for all practical values (the only difference is NaN, which is now treated as equal to itself — arguably a bug fix). This introduces a runtime dependency on `Set` (ES2015+). The compiled output emits `new Set()` directly with no polyfill, since the shared tsconfig already targets `es2019` — meaning `Set` was already assumed available by every other feature in the compiled output (arrow functions, `const`, etc.). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
7812ee6 to
7067397
Compare
yokuze
approved these changes
Mar 26, 2026
Contributor
yokuze
left a comment
There was a problem hiding this comment.
Ah right. I missed that. LGTM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
uniqusedindexOffor dedup, making it O(n²)Set.has/Set.addin bothstandardUniqanditerateeUniqfor O(n) amortized performanceCompatibility
This introduces a runtime dependency on
Set(ES2015+). Thecompiled output emits
new Set()directly with no polyfill.This is safe because the shared tsconfig already targets
es2019—Setwas already assumed available by every otherfeature in the compiled output.
Test plan
uniqtests pass (sorted, unsorted, iteratee)pass on the new one
🤖 Generated with Claude Code