[wp-trac] [WordPress Trac] #61782: Memoize wp_get_wp_version
WordPress Trac
noreply at wordpress.org
Mon Jul 29 01:40:23 UTC 2024
#61782: Memoize wp_get_wp_version
--------------------------+-----------------------------
Reporter: Cybr | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
`wp_get_wp_version()` currently requires a file whenever it's being
called. This file is being required every time the function is called.
{{{#!php
<?php
function wp_get_wp_version() {
require ABSPATH . WPINC . '/version.php';
return $wp_version;
}
}}}
We can memoize the return value using the static keyword. This will make
the file-requiring happen only once. At just 1000 iterations, this
memoization is already 160 times faster (from 0.1000s to 0.0006s execution
time).
{{{#!php
<?php
function wp_get_wp_version() {
static $wp_version;
if ( empty( $wp_version ) )
require ABSPATH . WPINC . '/version.php';
return $wp_version;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/61782>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list