Ajax takes 10x as long as it should/could

I have just hit my first serious issue with WordPress and for someone that enjoys Ajax this is a biggy.

I have an Ajax request that is taking 1.5 seconds to complete while using the Ajax API.

Read More

If I take the same exact code and run it with a custom script(no WordPress) the Ajax request takes merely 150 milliseconds. This is not an exaggeration

If you look at the very first comment of http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/ and the conversation that follows you will see that this slowness is caused by the fact that on your request, all of WP is loaded…

I am hoping that there is a solution out there that will make it possible to make Ajax requests while not loading all of WordPress.

What are your experiences with speeding up Ajax requests with WordPress?

Related posts

Leave a Reply

2 comments

  1. Yep, this is nasty issue that to have full WordPress environment you need to spend considerable time loading it.

    I’ve needed much better performance (for very dynamic incremental search feature) for work and what I went with is:

    1. Custom file as Ajax handler.
    2. SHORTINIT constant for limited WP core load.
    3. Very selectively loaded parts of core, only those needed for the task.

    This provides very limited environment, but performance is way way better and reasonable degree of compatibility with WP (starting with $wpdb) is retained.

    Here is start my loader file, not pretty but works for specific needs:

    <?php
    
    ini_set('html_errors', 0);
    define('SHORTINIT', true);
    
    require '../../../../wp-load.php';
    require( ABSPATH . WPINC . '/formatting.php' );
    require( ABSPATH . WPINC . '/meta.php' );
    require( ABSPATH . WPINC . '/post.php' );
    wp_plugin_directory_constants();
    
    // stuff goes here
    
  2. I found this and it sped up my ajax.

    function my_deregister_heartbeat() {
        global $pagenow;
    
        if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
             wp_deregister_script('heartbeat');
             wp_register_script('heartbeat', false);
         }
    }
    add_action( 'admin_enqueue_scripts', 'my_deregister_heartbeat' );