How to speed up admin-ajax.php in wordpress

Here’s the answer you’re searching for:

Ajax takes 10x as long as it should/could

Unfortunately, the solution there involves very detailed knowledge of what parts to load / what not to load and why stuff would break. Reading through github issues / comments, it seems this doesn’t work with HTTPS.

Otherwise, you really can’t speed it up, I’ve researched this extensively.

But may I suggest the REST API? The RA is amazing for just pulling data that is public. Think products, posts and any other information that’s just informational.

This has sped up my scripts by a ton when I decided to switch over.

Give it a try, see if it fits your use case. From the looks of it, it does. You’re not disclosing anything, so a rest endpoint with read-only functions would suit you.

Here are my 1:1 tests of Rest API vs. AJAX in my case.

I’m simply retrieving some data that I’ll then use to display –

WP AJAX:

function get_block_help_data(block_identifier) {

    return jQuery.ajax({
        url: block_help_data.ajax_url,
        type: 'POST',
        data: {
            action: 'parse_block_help_request',
            security: block_help_data.ajax_nonce,
            block_identifier: block_identifier
        },
    });
}

Implemented in the back-end:

add_action( 'wp_ajax_parse_block_help_request', array( $this, 'parseBlockHelpRequest' ) );

public function parseBlockHelpRequest()
{
    check_ajax_referer( 'block_help_nonce', 'security' );
    $block_identifier = sanitize_text_field( $_POST['block_identifier'] );

    //Check if the data is what we need.
    if( ( empty( $block_identifier ) || is_wp_error( $block_identifier ) ) ) {
        wp_send_json( 'Invalid data.', 500 );
        return;
    }

    wp_send_json( DataPool::getBlockHelpItem( $block_identifier ) );
}

REST:

function get_block_help_data(block_identifier) {

    return jQuery.ajax({
        url: block_help_data.rest_link + block_identifier,
        type: 'GET',
        dataType: 'JSON'
    });
}

Implemented in the back-end:

class BlockHelpREST extends \WP_REST_Controller
{
    /**
     * Holds the namespace for the REST endpoint.
     *
     * @var string
     */
    protected $block_help_namespace="block_help/v";

    /**
     * Holds the version for the REST endpoint.
     *
     * @var string
     */
    protected $block_help_version = '1';

    public function __construct()
    {
        $this->hookRestRouteToServer();
    }

    /**
     * Registers the main routes for the Block Help REST API.
     */
    public function registerRoutes() {
        $namespace = $this->block_help_namespace . $this->block_help_version;
        $base="block_identification";

        register_rest_route(
            $namespace, "https://wordpress.stackexchange.com/" . $base,
            array(
                array(
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => array( new DataPool, 'getBlockHelpItemByREST' ),
                    //'permission_callback' => array( new Privileges, 'canLoadSprout' )
                )
            )
        );
    }

    public function hookRestRouteToServer(){
        add_action( 'rest_api_init', array( $this, 'registerRoutes' ) );
    }
}

Over 20-30 runs (I know, not enough, but I can clearly see with the eye that WP AJAX is a tad bit slower):

The REST call took: MOSTLY 150ms with one high of 215.

The API call always, without exception, took at least ~400-500ms.

Ignoring that one high, this is incredible gains, but do keep in mind, the sample is small. I’ll keep testing and update because it, indeed, seems too good to be true.