Site AI Auditαπό την Internet Solutions

Defer vs Async: How to Load JavaScript Without Slowing Pages

20 Αυγούστου 20267 λεπτά ανάγνωσηςΤαχύτητα ιστοτόπου
Defer vs Async: How to Load JavaScript Without Slowing Pages

Short answer: Both defer and async let the browser download a script without pausing HTML parsing. A deferred script runs after the HTML has been parsed, in the order the scripts appear, just before the DOMContentLoaded event. An async script runs as soon as it finishes downloading, in no guaranteed order, possibly interrupting parsing. Use defer for most site scripts, especially ones that depend on each other or on the page structure, and async for independent scripts such as analytics.

Two small attributes on the <script> tag decide whether a page appears quickly or stares at visitors with a blank screen while scripts download. They are among the cheapest speed fixes available, but they also cause many “the menu stopped working” support tickets when applied carelessly. Understanding exactly what each attribute does takes five minutes and saves hours of debugging.

What happens with a plain script tag

When the browser’s HTML parser meets <script src="app.js"></script> without attributes, it stops. It downloads the file, executes it, and only then continues parsing the rest of the HTML. The reason is historical: a script might call document.write and insert new HTML at that exact position, so the parser cannot safely continue without running it.

Modern browsers soften this slightly with a preload scanner that looks ahead and starts downloading other resources early, but the parser itself still waits, and nothing below the script can be rendered until it has run. A few such scripts in the head, each from a different server, can add seconds of delay on a mobile connection.

How defer works

With <script src="app.js" defer></script>, the browser:

  1. starts downloading the script in parallel while continuing to parse the HTML;
  2. waits until the whole document has been parsed;
  3. executes deferred scripts in the order they appear in the document;
  4. then fires the DOMContentLoaded event.

Because execution happens after parsing, deferred scripts can safely access any element on the page. Because order is preserved, a deferred library such as jQuery will run before a deferred script that depends on it, as long as it appears first. The defer attribute only works for external scripts with a src; it is ignored on inline scripts.

How async works

With <script src="analytics.js" async></script>, the browser:

  1. starts downloading the script in parallel while continuing to parse;
  2. executes it as soon as the download completes, pausing parsing briefly if parsing is still going on;
  3. makes no promise about order relative to other scripts or to DOMContentLoaded.

Async suits scripts that are self-contained: they do not depend on other scripts, nothing depends on them, and they do not need the full page to exist when they run. Analytics, some advertising tags and independent widgets fit this pattern. Many third-party vendors provide their snippets with async already set.

Side-by-side comparison

No attributedeferasynctype=”module”
Blocks HTML parsing while downloadingYesNoNoNo
When it executesImmediatelyAfter parsingAs soon as downloadedAfter parsing (unless async)
Order preservedYesYesNoYes
Can rely on full DOMOnly elements above itYesNot guaranteedYes
Typical useTiny critical inline codeSite scripts, libraries and their pluginsAnalytics, independent tagsModern application code

Which to use for common scripts

Common mistakes and how to avoid them

A worked example

Imagine a small company site whose head contains four external scripts without attributes: jQuery, a slider plugin that depends on jQuery, the theme’s main script that handles the mobile menu, and an analytics tag. Further down, the footer contains an inline snippet that starts the slider with a jQuery call. On a phone, the browser must download and run all four files before it can show the first heading.

A safe rewrite looks like this. jQuery, the slider plugin and the theme script all get defer, in that order, so they download in parallel and run in sequence once the HTML is parsed. The analytics tag gets async, because nothing depends on it. The inline slider snippet is wrapped in a DOMContentLoaded listener, which fires only after the deferred scripts have run, so jQuery exists when the snippet needs it. Nothing is removed, no functionality changes, and the head no longer blocks the first paint. The next step, if the slider only appears on the home page, would be to stop loading its script elsewhere.

Going further: loading after interaction and dynamic imports

Defer and async solve the blocking problem, but the code still runs early in the visit. For heavy features that most visitors do not use immediately, you can go further:

How to apply defer and async on WordPress

Since WordPress 6.3, developers can register scripts with a loading strategy of defer or async through the script registration API, and WordPress respects dependencies when doing so. Many themes and plugins now use it. For older code, caching and optimisation plugins offer settings to defer scripts, usually with exclusion lists. Whatever method you use, test the menu on mobile, sliders, forms, cookie consent and the checkout afterwards.

How to check your result

  1. View the page source and confirm the attributes on each script tag.
  2. Open the browser console and reload: errors like “jQuery is not defined” point to ordering problems.
  3. Run PageSpeed Insights and check that scripts no longer appear under render-blocking requests.
  4. Compare First Contentful Paint and LCP before and after in the lab results.

How Site AI Audit helps

Site AI Audit includes Google PageSpeed for mobile in every check, so render-blocking scripts and their effect on LCP and INP show up in the speed findings, together with server response time, compression and page weight. Each finding is explained in plain words with a concrete fix and ranked by impact. Check your site for free before and after you change your script loading.

Related reading

The bottom line

Plain script tags stop the page; defer and async do not. Use defer for most scripts because it keeps order and runs after the page is parsed, and use async for independent tools like analytics. Watch for inline code that depends on deferred files, never make dependent scripts async, and test interactive features after every change.

FAQ

What is the difference between defer and async?

Both download scripts without blocking HTML parsing. Deferred scripts run after parsing, in document order. Async scripts run as soon as they are downloaded, in no guaranteed order.

Should I defer jQuery?

You can, as long as every script that depends on jQuery is also deferred and placed after it, and no inline code calls jQuery before it loads. Test carefully, because many older themes and plugins use inline jQuery code.

Does defer improve Core Web Vitals?

It usually improves First Contentful Paint and often Largest Contentful Paint by removing scripts from the critical rendering path. It does not reduce the amount of JavaScript executed, so it helps INP less. Removing or delaying heavy scripts helps INP more.

Can I use defer on inline scripts?

No, the defer attribute has no effect on inline scripts without a src attribute. If inline code must wait for the page, wrap it in a DOMContentLoaded event listener or move it into an external deferred file.

Are module scripts deferred automatically?

Yes. Scripts with type=”module” behave like deferred scripts by default: they download in parallel and run after parsing, in order. Adding async to a module script makes it run as soon as it is ready instead.

#Core Web Vitals#JavaScript performance#Page speed
Ελέγξτε τον δικό σας ιστότοπο — δωρεάν.Τι να διορθώσετε στον ιστότοπό σας — και από πού να ξεκινήσετε.
Ξεκινήστε δωρεάν

Περισσότερα από το blog

Όλα τα άρθρα →
Internet Solutions

Περισσότερα από την ομάδα μας

Από την Internet Solutions. Δοκιμάστε και τα άλλα προϊόντα μας — το καθένα σας εξοικονομεί χρόνο με διαφορετικό τρόπο.

internet-solutions.net ↗
01Αυτόματες αναρτήσεις στα social
PostRSS

Οι νέες αναρτήσεις από τη ροή RSS σας πηγαίνουν αυτόματα σε Facebook, X, LinkedIn, Telegram και σε 60+ ακόμη δίκτυα.

Δωρεάν πλάνο · από το 2014Επίσκεψη →
02Ζωντανή συνομιλία AI για ιστοσελίδες
Talkmio

Η ιστοσελίδα σας απαντά στους επισκέπτες 24/7 από το δικό σας περιεχόμενο, στη γλώσσα τους.

Δωρεάν πλάνο · χωρίς κάρταΕπίσκεψη →
03AI βοηθός
Ask Mio

Συνομιλία, κώδικας, σχεδιασμός, γραφή και έρευνα. Το Mio επιλέγει το καλύτερο μοντέλο για κάθε εργασία.

Δωρεάν πλάνοΕπίσκεψη →
04AI αυτόματος πιλότος για blog και social
AI Blog Autopilot

Η AI γράφει άρθρα SEO 2.000–3.000 λέξεων και κοινοποιεί το καθένα σε 58+ κοινωνικά δίκτυα.

Τα 3 πρώτα άρθρα δωρεάνΕπίσκεψη →
05Σε βάθος SEO crawl
Site SEO AI Audit

Πλήρες SEO crawl σε 7 τομείς, μαζί με την ορατότητα στην αναζήτηση AI, με διορθώσεις ταξινομημένες κατά αντίκτυπο.

Ο πρώτος έλεγχος δωρεάνΕπίσκεψη →
06Ροές RSS και προϊόντων
RSS Feed Creator

Δημιουργήστε RSS από οποιαδήποτε ιστοσελίδα, καθώς και ροές προϊόντων για Google και Meta που ενημερώνονται μόνες τους.

Δωρεάν πλάνοΕπίσκεψη →
07Ανάπτυξη ιστοσελίδων και SEO
Internet Solutions

Ιστοσελίδες, e-shops και εξειδικευμένα συστήματα — τα σχεδιάζει, τα αναπτύσσει και τα υποστηρίζει η ομάδα μας.

Από το 2011Επίσκεψη →
Site AI Audit
Επισκόπηση απορρήτου

Αυτός ο ιστότοπος χρησιμοποιεί cookies ώστε να σας προσφέρουμε την καλύτερη δυνατή εμπειρία. Οι πληροφορίες των cookies αποθηκεύονται στον browser σας και εξυπηρετούν λειτουργίες όπως την αναγνώρισή σας όταν επιστρέφετε και τη βοήθεια προς την ομάδα μας να καταλάβει ποιες ενότητες του ιστοτόπου βρίσκετε πιο ενδιαφέρουσες και χρήσιμες.