perf: Add max depth limits and optimize circular reference detection#277
Merged
perf: Add max depth limits and optimize circular reference detection#277
Conversation
- Add configurable depth limits to prevent stack overflow and O(n²) degradation: - maxFormatDepth (default: 50): Limits formatting recursion depth - maxCompareDepth (default: 100): Limits deep equality comparison depth - maxCompareCheckDepth (default: 50): Limits circular reference check iterations - Refactor _deepEqual to require context parameter (never optional): - Ensures configuration flows through deep equality checks properly - Optimize circular reference detection for better performance: - Search backwards through visited list (better cache locality) - Respect maxCompareCheckDepth to prevent O(n²) behavior in deep structures - Update _formatValue to respect maxFormatDepth limit: - Prevents pathological formatting of extremely nested objects - Treats excessive nesting as circular references for safety - Add comprehensive tests for depth limit configuration and behavior - Update copilot-instructions.md with refined @SInCE tag guidance **Critical fixes:** - Fixes regression where _deepEqual created null context - Prevents stack overflow with deeply nested structures - Maintains backward search optimization (better performance than forward search)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces configurable recursion/depth limits to harden tripwire’s formatting and deep-equality logic against stack overflows and pathological performance in deeply nested structures, while also refactoring internal deep-equality helpers to always receive configuration context.
Changes:
- Add
maxFormatDepth,maxCompareDepth, andmaxCompareCheckDepthconfiguration options with defaults. - Optimize circular reference detection (backward scan + bounded scan window) and enforce comparison depth limits.
- Add tests validating default values and basic configurability of the new depth settings.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| core/test/src/config/maxDepth.test.ts | Adds test coverage for default depth settings and configurability. |
| core/src/internal/_formatValue.ts | Adds max formatting depth guard and backward circular-reference scan. |
| core/src/interface/IConfig.ts | Documents and exposes new depth-related configuration options. |
| core/src/config/defaultConfig.ts | Sets default values for the new depth-related config options. |
| core/src/assert/ops/includeOp.ts | Updates deep-include operations to pass context into _deepEqual. |
| core/src/assert/funcs/nested.ts | Updates nested assertions to pass context into _deepEqual. |
| core/src/assert/funcs/members.ts | Improves deep-members matching and passes context into _deepEqual. |
| core/src/assert/funcs/keysFunc.ts | Updates deep-keys comparisons to pass context into _deepEqual. |
| core/src/assert/funcs/equal.ts | Enforces compare depth limits, optimizes circular detection, and makes _deepEqual require context. |
| common/config/rush/npm-shrinkwrap.json | Updates locked dependency versions. |
| .github/copilot-instructions.md | Refines repository guidance for selecting @since versions. |
Files not reviewed (1)
- common/config/rush/npm-shrinkwrap.json: Language not supported
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #277 +/- ##
==========================================
+ Coverage 91.43% 91.54% +0.10%
==========================================
Files 78 78
Lines 3445 3454 +9
Branches 886 885 -1
==========================================
+ Hits 3150 3162 +12
+ Misses 295 292 -3
🚀 New features to boost your workflow:
|
nevware21-bot
approved these changes
Feb 17, 2026
Contributor
nevware21-bot
left a comment
There was a problem hiding this comment.
Approved by nevware21-bot
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.
Add configurable depth limits to prevent stack overflow and O(n²) degradation:
Refactor _deepEqual to require context parameter (never optional):
Optimize circular reference detection for better performance:
Update _formatValue to respect maxFormatDepth limit:
Add comprehensive tests for depth limit configuration and behavior
Update copilot-instructions.md with refined @SInCE tag guidance
Critical fixes: