To address unused JavaScript, first use tools to identify code that does not run during page load. Then ask a developer whether that code is no longer needed or is used only for certain interactions, pages, or regional conditions. “Unused during page load” does not mean “safe to delete.” Remove only code confirmed to be unnecessary. For code that is still needed but does not have to run immediately, consider selective loading or code splitting to reduce the initial load while preserving essential functionality.
Use Lighthouse to identify scripts, then Coverage to examine the details
web.dev recommends starting with Lighthouse’s analysis of execution time for individual JavaScript files to identify scripts worth considering for code splitting. Next, use the Coverage tool in Chrome DevTools to see which code is unused during page load.
Lighthouse helps identify scripts that contribute to execution overhead, while Coverage shows which code within the files is unused. Coverage results are a starting point for discussion with developers, not a list of code to delete outright. When you see unused sections, check their purpose and the conditions that trigger them. The label alone does not mean they serve no purpose anywhere on the site.
Decide what to remove, retain, or load selectively based on how it is used
A 2023 Screaming Frog article described a case in which JavaScript flagged as “unused” actually checked the region and then displayed the appropriate translated content. Instead of removing this functionality outright, the approach was to ask developers whether they could reduce reliance on JavaScript or load it only when needed.
- Confirmed to be unnecessary: Before deleting files or code, ask technical staff to confirm that they are no longer needed and safe to remove.
- Needed only under specific conditions: Preserve essential functionality and assess whether it can be loaded only when needed, rather than loading every version at once.
- Third-party scripts: The recommendation in that case was to first check that scripts were up to date and installed only on pages that needed them—not to remove all third-party scripts.
People unfamiliar with coding should not directly edit code flagged in test results. Semrush’s related guidance also cautions that this kind of optimization is best done with help from a web developer to avoid accidentally removing functionality and breaking the site.
Code needed later can be split, but smaller chunks are not always better
Code splitting separates JavaScript needed for the initial load from code that can be loaded later. According to web.dev, code needed later can usually wait until users interact with the relevant elements. This can be implemented through dynamic import(). Code splitting addresses when to load code, rather than treating functionality that is still needed as dead code to be deleted.
Splitting also involves trade-offs: smaller files can make it easier to reuse cached content on return visits, but may compress less effectively. They may also increase network round trips when the browser has no cached copies. The goal should therefore be to balance caching efficiency, compression, and script evaluation time, rather than simply aiming for more, smaller files.
Handling unused JavaScript: perspectives from across the web
What should you do when shared templates make every page load code it does not need?
Start by checking whether shared templates include code that some pages do not need, then assess whether to remove unnecessary parts or create more specific template variants. This may reduce the amount of data transferred, but shared code may serve other pages. Before making changes, confirm each template’s actual requirements to avoid affecting existing functionality.
Do unused plugins or apps always need to be kept?
Not necessarily. Take stock of plugins, apps, and features that are no longer used, and consider disabling or removing them to reduce code that may load with the site. Online stores, in particular, can weigh whether a feature helps customers shop. Before making changes, confirm that it does not support other essential functionality; do not judge solely by its name.
Do you have to use tree shaking to remove unused code?
Not necessarily. Tree shaking typically helps remove unused code through an automated process and is one way to reduce file sizes, but its implementation depends on the site’s build environment. One article cautions that the process can be complex in some systems and may not be worth the time involved, so assess the implementation cost first.
What should you watch for when adjusting webpack’s code-splitting settings?
Watch for shared code being bundled more than once. In web.dev’s example, setting webpack to splitChunks: false and disabling SplitChunksPlugin caused the shared my-function.js to be included in both main.js and form-validation.js. This was the outcome in that example, not an indication that every project will be bundled the same way.
Should you keep checking for unused JavaScript after addressing it once?
You can include speed testing in routine maintenance. One article recommends using PageSpeed Insights or GTmetrix monthly to check for unused JavaScript, unoptimized images, and render-blocking resources. Test results can help prioritize follow-up checks, but you still need to confirm whether flagged code can actually be removed.
References
- Code-split JavaScriptweb.dev — Discusses: What should you watch for when adjusting webpack’s code-splitting settings?
- So You’ve Found JavaScript Issues in Your Site Crawl. What Now?www.screamingfrog.co.uk · 2023-09-01T10:57:38+00:00 — Source used in the article above.
- 網站速度優化對SEO的影響,從Google Page Speed Insights切入www.awoo.ai · 2019-05-14T03:28:00+00:00 — Discusses: What should you do when shared templates make every page load code it does not need?; Do unused plugins or apps always need to be kept?
- Core Web Vitals (核心頁面指標) 是什麼及如何優化-完整版tenten.co · 2022-06-30T16:51:16.000Z — Discusses: Do you have to use tree shaking to remove unused code?
- SEO重點項目:網站速度測試、速度優化指南www.yesharris.com · 2023-10-04T15:36:05+08:00 — Discusses: What should you do when shared templates make every page load code it does not need?; Do unused plugins or apps always need to be kept?
- SEO Tasks to Boost Your Site's Performance and Usabilitywww.semrush.com · 2025-05-05T10:15:00Z — Source used in the article above.
- The Ultimate Monthly Website Maintenance Checklistneilpatel.com · 2025-07-16T19:00:00+00:00 — Discusses: Should you keep checking for unused JavaScript after addressing it once?
- Shopify Site Speed Optimization: 6 Tips to Improve Your Sitewww.semrush.com · 2024-01-29T10:53:00Z — Discusses: Do unused plugins or apps always need to be kept?




