⚡️ Speed up function findDuplicateIndex by 14%#31
Open
codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Open
⚡️ Speed up function findDuplicateIndex by 14%#31codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
findDuplicateIndex by 14%#31codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Conversation
The optimized code achieves a **14% runtime improvement** through three key micro-optimizations that reduce per-iteration overhead: **What Changed:** 1. **Direct duplicate detection**: Replaced size-tracking logic (`_uniqSet.size > currSetSize`) with immediate `Set.has()` check before insertion. This eliminates the need to track and compare set sizes on every iteration. 2. **Cached `JSON.stringify` reference**: Storing `JSON.stringify` in a local variable (`stringify`) avoids repeated property lookups on the global `JSON` object across iterations. 3. **Cached array length**: Using `len = arr.length` in the for-loop initialization eliminates redundant length property access on each iteration. **Why It's Faster:** The original implementation performed unnecessary work on every loop iteration: - Unconditionally added elements to the set even when duplicates existed - Compared set sizes after every insertion - Accessed `JSON.stringify` and `arr.length` properties repeatedly The optimized version short-circuits immediately upon finding a duplicate (via `Set.has()`) and reduces property access overhead through caching. For duplicate detection, checking membership before insertion is more efficient than comparing sizes after insertion. **Performance Characteristics:** Based on the annotated tests, this optimization excels with: - **Large arrays with all unique elements** (1000 items): 62-168% faster due to reduced per-iteration overhead compounding over many iterations - **Primitive value arrays**: 15-138% faster on simple test cases - **Early duplicate detection**: Maintains fast early-exit behavior Some complex object tests show minor regressions (up to 48% slower for deeply nested structures), likely due to the additional local variable overhead being more noticeable when `JSON.stringify` dominates runtime. However, the overall **14% runtime improvement** demonstrates that the optimization benefits typical workloads, particularly those with larger arrays where the per-iteration savings accumulate significantly. The optimization preserves exact functional behavior, including handling of edge cases (NaN, undefined, circular references, sparse arrays).
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.
📄 14% (0.14x) speedup for
findDuplicateIndexinapp/client/src/workers/Evaluation/helpers.ts⏱️ Runtime :
1.50 milliseconds→1.31 milliseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 14% runtime improvement through three key micro-optimizations that reduce per-iteration overhead:
What Changed:
Direct duplicate detection: Replaced size-tracking logic (
_uniqSet.size > currSetSize) with immediateSet.has()check before insertion. This eliminates the need to track and compare set sizes on every iteration.Cached
JSON.stringifyreference: StoringJSON.stringifyin a local variable (stringify) avoids repeated property lookups on the globalJSONobject across iterations.Cached array length: Using
len = arr.lengthin the for-loop initialization eliminates redundant length property access on each iteration.Why It's Faster:
The original implementation performed unnecessary work on every loop iteration:
JSON.stringifyandarr.lengthproperties repeatedlyThe optimized version short-circuits immediately upon finding a duplicate (via
Set.has()) and reduces property access overhead through caching. For duplicate detection, checking membership before insertion is more efficient than comparing sizes after insertion.Performance Characteristics:
Based on the annotated tests, this optimization excels with:
Some complex object tests show minor regressions (up to 48% slower for deeply nested structures), likely due to the additional local variable overhead being more noticeable when
JSON.stringifydominates runtime. However, the overall 14% runtime improvement demonstrates that the optimization benefits typical workloads, particularly those with larger arrays where the per-iteration savings accumulate significantly.The optimization preserves exact functional behavior, including handling of edge cases (NaN, undefined, circular references, sparse arrays).
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
📊 Performance Profile
View detailed line-by-line performance analysis
To edit these changes
git checkout codeflash/optimize-findDuplicateIndex-ml24bvuaand push.