[wp-trac] [WordPress Trac] #46469: wp_script_is() is super slow

WordPress Trac noreply at wordpress.org
Tue Mar 12 03:13:21 UTC 2019


#46469: wp_script_is() is super slow
---------------------------+-----------------------------
 Reporter:  superdav42     |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Script Loader  |    Version:  5.0
 Severity:  normal         |   Keywords:
  Focuses:  performance    |
---------------------------+-----------------------------
 Because of the `WP_Dependencies::query()` recursively transverses scripts'
 dependencies plugins checking if a script handle has been registered using
 `wp_script_is()` can be very slow if scripts are registered with more
 complex dependencies. This will become more of a problem as more plugins
 start implementing Gutenberg blocks and register scripts with dependencies
 on Gutenberg components. As an example this simple plugin will show how
 the slow down can happen:

 {{{#!php
 <?php
 /*
 Plugin Name: Show slow deps
 */

 add_action(
         'enqueue_block_assets',
         // Some plugins regigister scripts which depend on wordpress
 components.
         function () {
                 wp_register_script(
                         'a-script-handle',
                         'https://www.example.com/somescript.js',
                         array(
                                 'wp-element',
                                 'wp-blocks',
                                 'wp-editor',
                                 'wp-components',
                                 'wp-data',
                         ),
                         '1.1.1',
                         true
                 );
                 wp_register_script(
                         'another-script-handle',
                         'https://www.example.com/anotherscript.js',
                         array(
                                 'wp-element',
                                 'wp-blocks',
                                 'wp-editor',
                                 'wp-components',
                                 'wp-data',
                         ),
                         '1.1.1',
                         true
                 );
                 wp_register_script(
                         'more-script-handle',
                         'https://www.example.com/anotherscript.js',
                         array(
                                 'wp-element',
                                 'wp-blocks',
                                 'wp-editor',
                                 'wp-components',
                                 'wp-data',
                         ),
                         '1.1.1',
                         true
                 );

                 // Same plugin or others check if a script is registered.
                 for ( $i = 1; $i < 20; $i++ ) {
                         wp_script_is( 'some-other-plugin-again' ); // This
 call is super slow.
                 }
         }
 );
 }}}

 If you installed this plugin and ran a code profiler on it you'd see
 `WP_Dependencies::recurse_deps()` being called 50,000+ time and take over
 100ms to do so.

 The way the recursion happens the same handles will be checked thousands
 of times. WP_Dependencies should probably be updated to keep a flattened
 array of scripts which have been queued including each queue script and
 their deps for faster lookup in `WP_Dependencies::query()`

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46469>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list