Making Autoptimize faster

One of the big changes in Autoptimize 2.0 (estimated released between Christmas & New Year) is a significant improvement in the minification speed (30% faster should be no exception). As a quick reminder, this is what Autoptimize did until now;

  1. extract code from HTML & remove original references
  2. aggregate all code into one string
  3. check if a minified version of that string exists in cache
  4. if not in cache;
    1. minify that string
    2. store the result in cache
  5. inject reference to cached autoptimized code in HTML

It is the actual minification in step (4) which can slow Autoptimize down (hence the importance of making sure your cached files are reusable). In Autoptimize 2.0 above logic was changed to improve performance;

  1. extract code from HTML & remove original references
  2. aggregate all unminified code into one string, but only put a reference to already minified files (*min.css and *min.js)
  3. check if a minified version of that string exists in cache
  4. if not in cache;
    1. minify that string
    2. replace references to minified files with (slightly optimized) contents
    3. store the result in cache
  5. inject reference to cached autoptimized code in HTML

As the to-be-minified string is smaller, the JS- & CSS-minifiers have less code to optimize, indeed speeding up the process significantly. Additionally this also reduces the chances of problems with the re-minification of already minified code (e.g. p. So nothing but advantages, right?
Now this was tested rather thoroughly and all known kinks have been ironed out, but If this “injected minified code late”-approach does not work in your context, you can simply disable it by hooking into the API and setting the autoptimize_filter_js_inject_min_late and/ or autoptimize_filter_css_inject_min_late filters to false (use code snippets rather then adding it to your functions.php);

add_filter('autoptimize_filter_js_inject_min_late','no_late_inject');
add_filter('autoptimize_filter_css_inject_min_late','no_late_inject');
function no_late_inject() {
	return false;
}

17 thoughts on “Making Autoptimize faster”

    • There’ll be no automatic cache clearing in AO 2.0 Rudra, still afraid that would create more problems then what such a mechanism would actually solve. There will however be a daily check on cache size + a warning on the admin-pages if the size goes over 0.5 GB.

      Reply
  1. Thanks Frank, I look forward to checking this out next week when I get a chance. I see that you’ve just released it (happy Boxing Day and best wished for NYE!)
    When you want to hook a function returning true, false, 0, null, an empty string, an empty array, you can utilise existing functions from wp-includes/functions.php — like this:

    add_filter('autoptimize_filter_js_inject_min_late','__return_false');
    add_filter('autoptimize_filter_css_inject_min_late','__return_false');

    cheers,
    Ross

    Reply
  2. Great…!!!! just installed the updated 2.0 version for my clients after backing up the sites. Working flawlessly. Didn’t have to revert back to the backed up sites.. 😉
    For my clients I use Nginx, PHP-FPM and MariaDB stack. So in an EMP stack no issues at all, no 502 errors. Personally I will always remain with AO develpoment version…!!!! 😉

    Reply
  3. I just installed version 2.0 and finally all is working now on my site. Just great, I tried the plugin half a year ago but ran into problems I think were related to the theme I used.
    Anyway, everything’s up and working great.
    Thank you so much for this awesome plugin Frank!
    Koen.

    Reply
  4. Thanks a ton, Frank, for maintaining Autoptimize, and making it available on the WordPress repo. This is without question the most important plugin for speed — for those of us who value optimization (which should be all of us).
    Much appreciated!

    Reply

Leave a Reply to frank Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.