Make WordPress Core

Changeset 55851


Ignore:
Timestamp:
05/23/2023 09:58:56 PM (9 months ago)
Author:
SergeyBiryukov
Message:

General: Improve performance of the _wp_array_get() function.

When using a block theme, _wp_array_get() is the most called function on the front end of a site.

This commit makes a few minor performance optimizations, which add up to a noticeable improvement.

Follow-up to [49135], [49143], [49580].

Props aristath, jrf, afercia, costdev, swissspidy, flixos90, spacedmonkey, mukesh27, samiamnot, SergeyBiryukov.
Fixes #58376.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r55703 r55851  
    49484948
    49494949    foreach ( $path as $path_element ) {
    4950         if (
    4951             ! is_array( $input_array ) ||
    4952             ( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) ||
    4953             ! array_key_exists( $path_element, $input_array )
     4950        if ( ! is_array( $input_array ) ) {
     4951            return $default_value;
     4952        }
     4953
     4954        if ( is_string( $path_element )
     4955            || is_integer( $path_element )
     4956            || null === $path_element
    49544957        ) {
    4955             return $default_value;
    4956         }
    4957         $input_array = $input_array[ $path_element ];
     4958            /*
     4959             * Check if the path element exists in the input array.
     4960             * We check with `isset()` first, as it is a lot faster
     4961             * than `array_key_exists()`.
     4962             */
     4963            if ( isset( $input_array[ $path_element ] ) ) {
     4964                $input_array = $input_array[ $path_element ];
     4965                continue;
     4966            }
     4967
     4968            /*
     4969             * If `isset()` returns false, we check with `array_key_exists()`,
     4970             * which also checks for `null` values.
     4971             */
     4972            if ( array_key_exists( $path_element, $input_array ) ) {
     4973                $input_array = $input_array[ $path_element ];
     4974                continue;
     4975            }
     4976        }
     4977
     4978        return $default_value;
    49584979    }
    49594980
Note: See TracChangeset for help on using the changeset viewer.