Skip to content

Conversation

AmadulHaque
Copy link
Contributor

Description

This pull request optimizes two string utility functions by avoiding repeated count() function calls within loop conditions. The changes improve performance by pre-calculating array counts before entering loops, eliminating unnecessary function calls during each iteration.

Changes Made

1. Str::apa() Method

  • Before: count($words) called on every loop iteration
  • After: Count stored in $wordCount variable for a single calculation

2. fileSize() Method

  • Before: count($units) called on every loop iteration
  • After: Count stored in $unitsCount variable for a single calculation

Performance Impact

These optimizations provide measurable performance benefits, especially when processing:

  • Long strings with many words in the apa() method
  • Large arrays of units in the fileSize() method

The changes maintain identical functionality while reducing computational overhead.

Code Quality

  • Maintains full backward compatibility
  • No changes to external API or behavior
  • Improved code efficiency without sacrificing readability
  • Follows established optimization patterns

Testing

All existing tests continue to pass, confirming that the functionality remains unchanged while performance is enhanced.

AmadulHaque and others added 3 commits August 27, 2025 23:40
This commit optimizes loops within the `Str::apa` and `Number::fileSize` methods by pre-calculating the array count and storing it in a variable.

Previously, the `count()` function was called on each iteration, leading to minor performance overhead. This change avoids these redundant function calls.
As an initial application of the tool, the `Number::fileSize` method has been refactored for performance. The array count is now cached in a variable before the loop to avoid calling `count()` on every iteration.
@taylorotwell taylorotwell merged commit b51cb93 into laravel:12.x Aug 27, 2025
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants