Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$scripts = $metadata[ $metadata_field_name ];
$processed_scripts = array();
if ( is_array( $scripts ) ) {
for ( $index = 0; $index < count( $scripts ); $index++ ) {
$script_count = count( $scripts );
for ( $index = 0; $index < $script_count; $index++ ) {
Comment on lines +680 to +681

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

Suggested change
$script_count = count( $scripts );
for ( $index = 0; $index < $script_count; $index++ ) {
foreach ( range( 0, count( $scripts ) - 1 ) as $index ) {

However, given that range() doesn't use a generator, this means that the full array has to be in memory. But in reality, it's not many!

So I think this is a better approach.

@mukeshpanchal27 mukeshpanchal27 Aug 7, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gemini share response:

These two approaches use entirely different paradigms—and in PHP, the second option (foreach with range()) introduces severe memory overhead and hidden performance issues.

Here is how they compare and why you should avoid range() for looping over arrays.


Comparison

  • for loop (Option 1): Fast and memory-efficient. Calculates array length once ($script_count) and iterates using standard integer incrementation.
  • foreach( range(...) ) (Option 2): Creates an entire second array in memory filled with integers from 0 to count($scripts) - 1 before the loop even starts.

Key Issues with foreach( range(...) )

1. Memory Overhead
If $scripts contains 10,000 items, range(0, 9999) allocates a brand-new array of 10,000 integers in RAM just to serve as the loop counter.

2. Off-by-One Bugs on Empty Arrays
If $scripts is empty (count($scripts) is 0), range(0, -1) will produce [0, -1]. Your loop will attempt to run twice instead of zero times, leading to Undefined array key warnings.

3. Unnecessary Complexity
Iterating over array indices with foreach + range defeats the purpose of foreach.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many items are we talking about here, though?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The theoretical performance concern would be addressed by a generator, which I noted above that PHP doesn't use unfortunately. Nevertheless, I don't believe the number of items is particularly large.

That said, I don't feel strongly about using range().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That said, can we mark this conversation as resolved?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't hurt, and changing it can reduce code smell so others don't raise this in the future.

I would suggest, however, that the count initializer be placed inside the for loop, like this:

for (
    $index = 0, $script_count = count( $scripts );
    $index < $script_count;
    $index++ 
) {

Or for a common idiom for naming:

for ( $i = 0, $len = count( $scripts ); $i < $len; $i++ ) {

This can be found in core (via 3rd party libraries), for example:

for ($i = 1, $len = count($bytes); $i < $len; $i++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another example:

for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {

There quite a few.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially i propose that 83e095b but based on the #12837 (comment) feedback i apply that suggestion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest, however, that the count initializer be placed inside the for loop, like this:

Asking as a learning opportunity: what would be the advantage? That way, count() is called at each loop iteration and the same variable is set again and again at each loop iteration. That seems completely unnecessary to me. If there are other places where this pattern is in used, then I'd argue it should be imporved there as well.
I know there are many occurrences of similar patterns, also on the JS side. That doesn't mean they are entirely OK.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this is finalized, I'll open a separate PR to address the other occurrences. That's my plan as a follow-up.

$result = register_block_script_handle(
$metadata,
$metadata_field_name,
Expand Down Expand Up @@ -711,7 +712,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$modules = $metadata[ $metadata_field_name ];
$processed_modules = array();
if ( is_array( $modules ) ) {
for ( $index = 0; $index < count( $modules ); $index++ ) {
$module_count = count( $modules );
for ( $index = 0; $index < $module_count; $index++ ) {
$result = register_block_script_module_id(
$metadata,
$metadata_field_name,
Expand Down Expand Up @@ -747,7 +749,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$styles = $metadata[ $metadata_field_name ];
$processed_styles = array();
if ( is_array( $styles ) ) {
for ( $index = 0; $index < count( $styles ); $index++ ) {
$style_count = count( $styles );
for ( $index = 0; $index < $style_count; $index++ ) {
$result = register_block_style_handle(
$metadata,
$metadata_field_name,
Expand Down
3 changes: 2 additions & 1 deletion src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5386,7 +5386,8 @@ public function set_spacing_sizes() {

// If there are 7 or fewer steps in the scale revert to numbers for labels instead of t-shirt sizes.
if ( $spacing_scale['steps'] <= 7 ) {
for ( $spacing_sizes_count = 0; $spacing_sizes_count < count( $spacing_sizes ); $spacing_sizes_count++ ) {
$total_spacing_sizes = count( $spacing_sizes );
for ( $spacing_sizes_count = 0; $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) {
$spacing_sizes[ $spacing_sizes_count ]['name'] = (string) ( $spacing_sizes_count + 1 );
}
}
Expand Down
Loading