Refactor utility functions for performance and maintainability#1
Merged
Conversation
- Refactor dive() to build paths top-down, eliminating O(n) unshift in recursion and stack-overflow-prone spread push on large arrays - Refactor decircular() to use mutable push/pop path instead of creating new arrays per key per recursion level - Convert .includes() to Set.has() in order_sort/exclude/include for O(1) lookups instead of O(n*m) - Change entity() .map() to .forEach() to avoid throwaway array allocation - Add tests for stringify() and decircular() https://claude.ai/code/session_017xSbcASVkxy5qHKKzn7kKi
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
This PR refactors several utility functions in
src/util.tsto improve performance, code clarity, and maintainability. The changes focus on optimizing recursive operations, replacing array lookups with Set-based checks, and improving the circular reference detection algorithm.Key Changes
Refactored
dive()function: Extracted the recursive logic into a newdiveInternal()helper function that uses an explicitprefixarray parameter instead of manipulating array indices. This eliminates the need forunshift()operations and reduces array allocations.Added
hasOwnKeys()utility: Introduced a new helper function to check if an object has enumerable properties, replacing theObject.keys(child).length === 0check for better performance.Optimized
order_sort()function: Convertedkey_orderarray lookups to use aSetfor O(1) lookup performance instead of O(n) array includes checks.Optimized
order_exclude()andorder_include()functions: ReplacedArray.includes()calls withSet.has()for better performance when filtering items.Improved
decircular()function: Refactored to use a sharedpatharray that is mutated during traversal (with push/pop) instead of creating new arrays at each recursion level. This reduces memory allocations and improves performance for deeply nested objects.Minor fix in
entity()function: ChangedObject.entries().map()toObject.entries().forEach()since the return value was not being used.Added comprehensive tests: Added test cases for
stringify()anddecircular()functions to ensure correctness of the refactored code.Implementation Details
The
diveInternal()refactoring maintains the same behavior while improving efficiency by:The
decircular()optimization uses a closure-scopedpatharray that is mutated during traversal, significantly reducing garbage collection pressure for large object graphs.https://claude.ai/code/session_017xSbcASVkxy5qHKKzn7kKi