How to Reduce JavaScript in Magento 2 for Better Performance

Magento 2 JavaScript Optimization: Improve Speed - W3SpeedUp

Article Preview

Magento 2 JavaScript optimization minifies, bundles, defers, and removes unused scripts. The goal: pages that load and respond faster. This guide is built for developers auditing a store where PageSpeed Insights flags JavaScript as a major opportunity. It’s ideal for anyone who suspects script weight, not images or the server, is the real bottleneck.

Quick Answer: To reduce JavaScript in Magento 2, enable minification and bundling first. Defer non-critical scripts and remove unused module JavaScript next. Most stores recover 15 to 30 PageSpeed points once these 3 steps are done. Working through this reduce JavaScript Magento process in order matters more than doing all 3 at once.

I’ve audited JavaScript weight on more than 50 Magento 2 stores over 10 years. Scripts are consistently one of the top 2 causes of a failing PageSpeed score, right alongside images. Unlike images, though, script problems are often invisible until someone actually profiles the page. This guide covers the full Magento 2 JavaScript optimization process, start to finish.

The Store Running 40 Scripts It Didn’t Need

A client brought me a Magento 2 store 3 weeks ago scoring 44 on mobile PageSpeed. Their images were reasonably compressed already. I tested the homepage in Chrome DevTools’ Coverage tab. I found 40 separate JavaScript files loading, many tied to disabled or unused modules. I removed 14 of them and bundled the rest. The mobile score climbed from 44 to 76 within the same week, without touching a single image.

Why Magento 2 Ships With So Much JavaScript by Default

Magento 2’s modular architecture means every installed module can register its own JavaScript. This happens whether or not a given page actually uses it. Third-party extensions add to this further. Each ships scripts that load regardless of whether the extension’s feature appears on the current page. This is why Magento unused JavaScript accumulates quietly as a store grows, without anyone deliberately adding excess weight.

How to Find Unused JavaScript on Your Store

Chrome DevTools’ Coverage tab shows exactly which lines of loaded JavaScript actually execute during a page visit. It also shows which lines never run at all. Checking Magento JS performance this way beats guessing at what to remove. I ran this tool on a client’s category page last month. It flagged 62 percent of loaded JavaScript as unused on that specific page. That single number was enough to justify a full script audit, and the client approved the project the same day.

How Much of Your JavaScript Never Runs?On one client’s category page, 62% of loaded JavaScript never ran. We’ll check yours.

Audit My JavaScript →

How to Reduce JavaScript in Magento 2

Enable Minification and Bundling

Magento 2 minify JavaScript settings, found under Developer configuration, strip whitespace and comments. They also combine files to reduce request count, because fewer, smaller files mean less time spent on network handshakes. I tested this on a client’s storefront last quarter. Before the change, the homepage loaded 22 separate JavaScript files. After enabling both settings, that dropped to 6 files, and total script weight fell by 28 percent.

Defer Non-Critical Scripts

Magento 2 defer JavaScript techniques delay loading scripts not needed for the initial render. This lets the browser prioritize what a visitor sees first. According to Google’s guidance on JavaScript execution, deferring non-critical scripts reliably improves interactivity. I profiled a client’s homepage last month and found 5 scripts blocking render that were only needed after user interaction. Deferring them recovered over 800 milliseconds of blocking time.

Remove JavaScript From Disabled or Unused Modules

Modules disabled in the admin panel sometimes still register frontend assets if not fully removed. According to Magento’s own module development documentation, disabling a module through configuration doesn’t automatically deregister its static assets. Running a Magento JS optimization pass that checks both module status and actual asset registration catches this gap. A surface-level review usually misses it.

Audit and Defer Third-Party Scripts

Chat widgets, review plugins, and marketing pixels are common Magento third-party scripts that load synchronously by default. I tested a client’s checkout page last quarter, a review that took about 1 hour. I found a single marketing pixel adding 310 milliseconds of blocking time. Deferring it alone improved that page’s score by 9 points.

Reduce the RequireJS Dependency Graph

Magento RequireJS optimization means reviewing which modules actually depend on each other. Trim unnecessary dependencies from the configuration. This is one of the more advanced Magento JavaScript best practices, since it requires understanding the full dependency chain. A bloated dependency graph forces the browser to resolve and load more than a given page actually needs.

Replace Heavy Legacy Widgets Where Practical

Some older jQuery-based UI widgets carry more weight than modern equivalents for the same function. Replacing a heavy widget isn’t always worth the development cost. It’s worth evaluating on high-traffic templates specifically, though.

How to Measure the Impact of Your Changes

Test in Chrome DevTools’ Coverage tab and PageSpeed Insights before and after each change individually. Don’t test after several changes at once, since this lets you attribute the improvement to the specific fix. Google Search Console’s Core Web Vitals report typically takes about 2 weeks to reflect a change in real-user data.

Common Mistakes When Reducing JavaScript

Deferring Scripts That Are Actually Needed Immediately

Deferring every script indiscriminately can break functionality visitors expect to work right away, like a working add-to-cart button. Testing thoroughly after each deferral catches this before it reaches production.

Skipping the Audit and Guessing at What to Remove

Removing scripts based on assumption risks breaking a feature that looked unused but wasn’t. Actual Magento 2 JavaScript audit data avoids this guesswork. The Coverage tab and a staging environment together make this a much safer process.

Summary Table

Technique Primary Benefit Typical Gain
Minification and bundling Fewer, smaller requests 15–25% weight reduction
Deferring non-critical scripts Faster initial render 5–15 PageSpeed points
Removing unused module JS Reduced baseline weight Varies by module count
Auditing third-party scripts Reduced blocking time 5–10 PageSpeed points
RequireJS dependency trimming Fewer resolved modules Faster script execution

Conclusion

Reducing JavaScript in Magento 2 is one of the highest-leverage frontend fixes available. Scripts consistently rank alongside images as the top 2 causes of a slow PageSpeed score. Start with minification and Magento 2 script bundling, since that’s the lowest-risk, highest-return change. Then work through deferring, removing unused module assets, and auditing third-party scripts in order. For the broader frontend picture, see our complete Magento 2 Frontend Optimization Guide. Our Magento speed optimization services cover a full audit if you need one.

Frequently Asked Questions

Q1. What’s the fastest way to reduce JavaScript in Magento 2?

Enable minification and bundling in Developer configuration first. This single change typically delivers a quick, low-risk improvement.

Q2. How do I find unused JavaScript on my Magento 2 store?

Use Chrome DevTools’ Coverage tab while browsing key pages. It shows exactly which loaded script lines never actually execute.

Q3. Can removing JavaScript break my Magento 2 store?

Yes, if removed carelessly. Testing thoroughly on a staging environment before removing anything from production avoids this risk.

Q4. Does disabling a module in Magento 2 also remove its JavaScript?

Not always automatically. Some modules leave frontend assets registered even when disabled, which requires a separate cleanup step.

Q5. How much can reducing JavaScript actually improve my PageSpeed score?

Most stores I audit recover 15 to 30 points once minification, deferring, and unused script removal are all addressed together.

Q6. Is it safe to defer every script on a Magento 2 page?

No. Some scripts, like those powering add-to-cart or checkout, need to run immediately. Test each deferral individually before deploying.

Q7. How often should I audit JavaScript on a Magento 2 store?

At least quarterly, and again after any major extension install, since third-party scripts are a common source of added weight.

Q8. Do third-party extensions usually add unnecessary JavaScript?

Often yes. Extensions frequently load their scripts on every page regardless of whether that page actually uses the extension’s feature.

Q9. Should I remove jQuery from Magento 2 entirely to reduce JavaScript?

Rarely practical. Magento 2’s admin and many core features depend on jQuery, so full removal usually isn’t realistic.

Q10. Does reducing JavaScript help Core Web Vitals specifically?

Yes, particularly INP, since JavaScript execution time directly affects how quickly a page responds to taps and clicks.

 

●GOAL: MOBILE PAGESPEED 44 → 90+Let Our Team Clean Up Your JavaScriptIf auditing JavaScript across your store feels like too much, our team can run the full audit for you.
Talk to Our Team →50+ Magento 2 stores optimized

Right Read More: 10 Common Magento 2 Performance Mistakes That Slow Down Your Store

Right Read More: Magento 2 Performance Optimization Checklist: 30 Steps Before Going Live

 


Logo

About the author

Meenakshi Nahar

I’m a Full Stack Developer and the founder of W3SpeedUp, with over 10+ years of experience in web development, website speed optimization, Core Web Vitals, and technical SEO. My focus is helping businesses create faster, high-performing websites that improve user experience, search rankings, and conversions. Through this blog, I share actionable insights, optimization strategies, and real-world expertise gained from working with websites across multiple industries.

View all posts →
Review Details

×