WordPress Development
WordPress JavaScript Optimisation: Defer, Conditional Loading, and INP Improvement
JavaScript is the heaviest performance burden on most WordPress sites. Here is the complete optimisation framework — from auditing to deferral to replacing heavy scripts.
Simple Automation Solutions
··⌛ 9 min read
JavaScript is typically the heaviest performance burden on WordPress sites. Every plugin that adds scripts, every page builder that loads its framework, and every third-party tool that injects JavaScript contributes to INP (Interaction to Next Paint) and LCP (Largest Contentful Paint) scores. Here is how to audit, reduce, and optimise JavaScript on WordPress.
Why JavaScript matters more than ever for WordPress
Google’s shift from FID (First Input Delay) to INP (Interaction to Next Paint) as a Core Web Vitals metric in March 2024 made JavaScript performance significantly more important for SEO. INP measures how quickly your page responds to every user interaction — not just the first. Heavy JavaScript that blocks the main thread causes poor INP scores even on fast-loading pages.
| Metric | What JavaScript affects | Target |
|---|---|---|
| Largest Contentful Paint (LCP) | Render-blocking scripts delay first content render | Under 2.5 seconds |
| Interaction to Next Paint (INP) | Heavy JS blocks main thread, delaying interaction response | Under 200ms |
| Time to Interactive (TTI) | JS execution must complete before page is interactive | As low as possible |
| Total Blocking Time (TBT) | Long tasks on main thread block user interaction | Under 200ms |
Auditing JavaScript on your WordPress site
PageSpeed highlights specific JS issues: ‘Eliminate render-blocking resources’, ‘Reduce unused JavaScript’, ‘Avoid long main-thread tasks’. Each Opportunity item shows which scripts are causing the issue.
Record a page load in Chrome DevTools Performance tab. The flame chart shows exactly which scripts take longest to execute. Long Tasks (red diagonal lines) are your highest-priority optimisation targets.
In Chrome DevTools, open the Coverage tab (Ctrl+Shift+P, type Coverage). Load your page and see what percentage of each JavaScript file is actually executed. Files with 70%+ unused JS are candidates for deferred loading or removal.
Query Monitor’s Scripts panel shows every script enqueued on each page, which plugin or theme enqueued it, and where it loads (header or footer). Identify scripts loading in the header that could be deferred.
Method 1 — Defer non-critical JavaScript
Deferred JavaScript loads after the page has rendered, preventing it from blocking initial page display. Configure deferral in WP Rocket or other caching plugins:
- WP Rocket: File Optimisation › JavaScript › Load JavaScript Deferred. This adds the defer attribute to all applicable scripts automatically.
- W3 Total Cache: Minify › JS Minify › Load deferred. Similar functionality to WP Rocket.
- Manual defer via functions.php: hook the
script_loader_tagfilter to adddeferorasyncattributes to specific script handles. - Always test after enabling defer — some scripts depend on synchronous loading order and break when deferred.
Method 2 — Load scripts only where needed
Most plugin scripts load on every page even when the plugin functionality is only used on specific pages. A contact form plugin loading its validation script on your homepage is waste. Fix this with Asset CleanUp Pro or manual conditional enqueue:
Asset CleanUp scans every page of your site and shows which scripts are loaded. You can disable individual scripts on specific pages with a toggle.
A contact form plugin script should only load on your Contact page. A WooCommerce script should only load on shop, product, cart, and checkout pages.
In Asset CleanUp, mark scripts as disabled globally and enable only on relevant pages. Or use conditional loading in your plugin or theme: wrap wp_enqueue_script() calls in is_page(), is_single(), or is_woocommerce() checks.
Method 3 — Minify and combine JavaScript
Minification removes whitespace and comments from JS files, reducing file size. Combining merges multiple JS files into one, reducing HTTP requests. Both are available in caching plugins:
- WP Rocket: File Optimisation › Minify JS files and Combine JS files
- W3 Total Cache: Minify › JS Minify settings
- Combining JS is more fragile than minifying alone — test thoroughly after enabling, especially with Elementor, Divi, or other page builders that load many scripts
Method 4 — Replace heavy scripts with lighter alternatives
Some plugins are simply too heavy for what they do. Common replacements:
| Heavy solution | Lighter alternative | Savings |
|---|---|---|
| jQuery-based slider/carousel | CSS-only carousel or lightweight JS (Swiper.js) | 300-500KB removed |
| Full jQuery UI library | Use specific jQuery UI components only, or replace with vanilla JS | 100-200KB removed |
| Full Lodash/Moment.js | Use native JavaScript equivalents for date formatting and utilities | 50-150KB removed |
| Heavy social sharing plugin | CSS+HTML share buttons with no JS | 20-80KB removed |
| Full FontAwesome | Use only the icons you need via SVG sprites | 200-400KB removed |
Method 5 — Preload critical scripts
For scripts that are critical to your page rendering (your primary theme JavaScript, critical functionality), preloading tells the browser to fetch them earlier in the loading sequence:
Add <link rel='preload' as='script' href='/path/to/critical.js'> to your page head. WP Rocket’s link preloading feature handles this for scripts you designate as critical. Do not preload non-critical scripts — it defeats the purpose.
Need JavaScript performance optimisation for your WordPress site?
Simple Automation Solutions performs WordPress JavaScript audits and implements optimisations that improve INP, LCP, and Core Web Vitals scores for businesses worldwide.
Frequently asked questions
How do I know which JavaScript files are causing poor INP scores?+
Use Chrome DevTools Long Tasks API or the Web Vitals Chrome extension. In DevTools, record a page load and user interaction in the Performance panel. Look for Long Tasks (orange bars) that occur during user interactions. Each Long Task shows which script file was executing. The PerformanceObserver API can also log INP events with the associated script, identifying exactly which interaction and which code path is causing the delay.
Will removing jQuery break my WordPress site?+
WordPress includes jQuery by default and thousands of plugins depend on it. Removing jQuery outright will almost certainly break functionality. However, you can improve jQuery performance by loading it in the footer instead of the header (defer it via WP Rocket), ensuring plugins use the WordPress-bundled jQuery rather than loading their own version, and ensuring jQuery Migrate is disabled on production sites (it is only needed for legacy plugin compatibility testing). Modern WordPress development can use vanilla JavaScript for new code, but the existing jQuery dependency is rarely worth removing entirely.
What is the difference between defer and async for JavaScript?+
Both load scripts without blocking HTML parsing. The difference is in execution timing. Async scripts execute as soon as they are downloaded, potentially before HTML is fully parsed — they can arrive and execute in any order. Defer scripts execute after HTML parsing is complete, in the order they appear in the HTML. For WordPress themes and plugins that depend on the DOM being ready before executing, defer is usually the correct choice. Async is better for completely independent scripts like analytics or advertising tags that do not depend on DOM elements.
Simple Automation Solutions is a global digital product studio specialising in WordPress and Bubble.io. We serve founders, startups, and businesses worldwide — delivering production-ready websites built to rank, convert, and scale.
