Perf: Optimize Arr::get and Arr::exists for performance improvement #22
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
Optimizes
Arr::get()andArr::exists()methods to fix critical performance issues withArrayAccessobjects and improve efficiency for simple key lookups.Performance Issues Fixed
🚨 Critical: iterator_to_array() Performance Bug
Problem: Previous implementation called
iterator_to_array()on every iteration when working withArrayAccessobjects.Before:
After:
Impact:
✨ Simple Key Optimization
Added fast path for keys without dots (e.g.,
'foo',123):explode()calls🔒 Type Safety Improvement
offsetExists()which is guaranteed by theArrayAccessinterfaceArrayAccessobjects wereTraversable(not always true)Code Quality Improvements
array_key_exists()andoffsetExists()Test Coverage
Added 5 new test cases (23 total, up from 18):
All 68 tests pass across the entire test suite with zero regressions.
Backward Compatibility
✅ Fully backward compatible - all existing tests pass without modification
✅ API unchanged - same method signatures and behavior
✅ Null handling preserved - correctly handles null values in both arrays and ArrayAccess objects
Example Performance Impact
Files Changed
src/Arr.php- Performance optimizations forget()andexists()tests/ArrTest.php- Added 5 new test casesTesting