Use array_key_first() to read the first key of an array - #12785
Use array_key_first() to read the first key of an array#12785mukeshpanchal27 wants to merge 2 commits into
array_key_first() to read the first key of an array#12785Conversation
…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.
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
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$actionskey 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.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Trac ticket: https://core.trac.wordpress.org/ticket/65773
Replace
current( array_keys( $array ) )witharray_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$actionsarray 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.