Skip to content

Use array_key_first() to read the first key of an array - #12785

Open
mukeshpanchal27 wants to merge 2 commits into
WordPress:trunkfrom
mukeshpanchal27:perf/array-key-first
Open

Use array_key_first() to read the first key of an array#12785
mukeshpanchal27 wants to merge 2 commits into
WordPress:trunkfrom
mukeshpanchal27:perf/array-key-first

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Jul 31, 2026

Copy link
Copy Markdown
Member

Trac ticket: https://core.trac.wordpress.org/ticket/65773

Replace current( array_keys( $array ) ) with array_key_first( $array ). The former builds a complete array of every key only to read the first one and throw the rest away, which costs O(n) time and O(n) memory; array_key_first() reads the first bucket directly in constant time and allocates nothing.

wp_admin_bar_new_content_menu() runs on every admin page load and on every front-end load for logged-in users with the toolbar visible, and its $actions array grows with the number of registered post types.

Use of AI Tools


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…rray.

Replace `current( array_keys( $array ) )` with `array_key_first( $array )`. The
former builds a complete array of every key only to read the first one and throw
the rest away, which costs O(n) time and O(n) memory; `array_key_first()` reads
the first bucket directly in constant time and allocates nothing.

`wp_admin_bar_new_content_menu()` runs on every admin page load and on every
front-end load for logged-in users with the toolbar visible, and its `$actions`
array grows with the number of registered post types.

`array_key_first()` is available in PHP 7.3 and later, which is below the current
minimum supported version.

Props mukesh.
@mukeshpanchal27 mukeshpanchal27 self-assigned this Jul 31, 2026
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves performance in a couple of hot paths by replacing current( array_keys( $array ) ) with array_key_first( $array ), avoiding unnecessary full key-array allocation when only the first key is needed.

Changes:

  • Use array_key_first() for the first $actions key when building the “New” admin bar menu link.
  • Use array_key_first() to extract the first request ID key from a POSTed action array in privacy tools.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/wp-includes/admin-bar.php Avoids building an intermediate keys array when selecting the first $actions entry for the “New” menu URL.
src/wp-admin/includes/privacy-tools.php Avoids building an intermediate keys array when extracting the first request ID from $_POST['privacy_action_email_retry'].

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mukeshpanchal27
mukeshpanchal27 marked this pull request as ready for review July 31, 2026 05:50
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Soean Soean left a comment

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.

Looks good to me

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.

3 participants