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.

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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

Method 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' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x