How to Prevent Legacy JavaScript from Slowing Down Modern Browsers

watch 8m, 39s
views 2

08:56, 24.11.2026

Article Content
arrow

  • What exactly do we mean by “legacy JavaScript”?
  • Why the Old Code Format Is Still Costly
  • 5. Split the code based on when it’s used
  • 6. Help tree shaking remove dead code
  • 7. Check the final build, not the source code
  • 8. Set a performance budget
  • Don’t blindly remove old code
  • Conclusion

Imagine that a modern browser is given a simple task, but your builder adds a translator, a dictionary, and a suitcase full of spare tools to it. Chrome, Safari, or Firefox load all this baggage. Then the browser analyzes the code, compiles it, and executes it.

This is how legacy JavaScript silently slows down modern websites.

We often associate legacy JavaScript with old libraries or code that hasn’t been touched in a decade. In reality, the problem is often hidden in a brand-new project. Your code may use modern syntax, but the production build converts it to ES5 and adds dozens of polyfills for browsers that are virtually nonexistent among your audience.

Let’s take a look at how you can reduce this unnecessary overhead without breaking the site for real users.

What exactly do we mean by “legacy JavaScript”?

Legacy JavaScript refers to code that your tools have adapted for older browsers. This includes syntactic transformations, helper functions, and polyfills.

For example, Babel can convert an arrow function into a regular function. It can also replace classes with more complex constructs or add code to support Promises, `Array.from`, and other features.

Such changes make sense when your audience actually uses older browsers. The problem arises when a modern browser receives a compatibility bundle without a real need for it.

Chrome even has a separate Legacy JavaScript check in its performance tools. As of 2026, the check flags an issue when it finds more than 5 KiB of polyfills or transformations for features that already have broad support according to the Baseline criteria.

Why the Old Code Format Is Still Costly

File size matters, but network traffic only tells part of the story. The browser loads JavaScript, parses its structure, checks the syntax, compiles the code, and only then executes it. A large package creates more work at every stage. During the initial load, this work can delay the page’s response to clicks, text input, or scrolling.

The browser’s main thread executes most JavaScript tasks sequentially. It cannot simultaneously process a heavy script and respond quickly to user actions. Web performance experts define a task lasting more than 50 milliseconds as a long-running task. Such a task can make the interface sluggish or temporarily unresponsive.

The user doesn’t see the compilation happening. They see a button that isn’t responding.

1.Identify the browsers you actually support

Don’t start your optimization by removing polyfills. Start with the data. Check the browsers, operating systems, and devices in your web analytics. An enterprise product, a government portal, and a youth media site may have completely different audiences.

Browserslist lets you pass your target browsers to Babel, PostCSS, and other tools. You can also connect your site’s statistics from Google Analytics, Plausible, or New Relic. Then your configuration will be based on your audience, rather than on an abstract global ranking.

For a modern project, the Chrome team suggests considering the following query:

baseline widely available

This directs tools toward features that have been working in major browser engines for a long time. However, we recommend comparing this benchmark with your actual statistics.

A poor configuration often looks safe:

> 0%

In reality, it can force the compiler to generate code for nearly every known browser. Your modern users will pay for this excessive caution with performance.

2. Configure Babel for specific targets

@babel/preset-env analyzes the target browsers and selects only the appropriate transformations. When you set targets that are too old, Babel naturally generates older and heavier code. When you set up-to-date targets, Babel can preserve more of the modern syntax.

The Babel team has taken an important step in this direction. Babel 8 no longer compiles code to ES5 by default. The new version uses the standard Browserslist query, which at the time of release roughly corresponded to the ES2023 level. Developers can still choose ES5, but now they must do so intentionally.

Check your `babel.config.js`, `.babelrc`, or the `browserslist` section in `package.json`. The old rule may have carried over into your project from a template, a previous version of a framework, or an outdated corporate requirement.

We recommend asking the team a simple question: Who exactly needs this transformation?

If no one can name a specific browser, version, or audience share, the configuration needs to be reviewed.

3. Serve modern code to modern browsers

A single package for all browsers seems like a simple solution. However, this simplicity shifts the burden to the user.

You can create two build variants. Modern browsers will receive the modular package, while older browsers will load the compatible version.

<script type="module" src="/app.modern.js"></script>

<script nomodule src="/app.legacy.js"></script>

Browsers that support JavaScript modules will execute the first file. The `nomodule` attribute marks the second file as a fallback for browsers that do not support modules.

Modern tools can automate this process. For example, the official @vitejs/plugin-legacy creates separate legacy chunks and loads them only in browsers without native ESM support.

This approach maintains compatibility without burdening the main audience with unnecessary code.

4. Add only the necessary polyfills

A polyfill replicates a modern feature in a browser that doesn’t support it. It helps older environments but becomes redundant in modern ones.

Don’t import the full core-js package just because an old guide recommended it. The full package may include support for functions that your application doesn’t even use.

Babel maps target browsers to compatibility tables and can select transformations and polyfills based on your configuration. The latest version of Babel 8 has moved core-js injection management into a separate plugin, giving your team more precise control over polyfills.

We recommend checking three things:

  1. Which polyfills are included in the production bundle.

  2. Which browsers require them.

  3. Which features your code actually uses.

You can also load a specific polyfill dynamically only after verifying that the feature is needed. This approach reduces the size of the main bundle but adds complexity. Use it for large or rare polyfills, not for every minor feature.

5. Split the code based on when it’s used

Even modern JavaScript can slow down the browser if you load too much of it.

A product page shouldn’t immediately load the profile editor, admin panel, and report export module. The user may never open these features.

Dynamic `import()` allows you to load a module only when it’s needed:

button.addEventListener(“click”, async () => {

const { openReportBuilder } = await import(“./report-builder.js”);

openReportBuilder();

});

The bundler will create a separate chunk for the report module. The browser will not load, parse, or execute this code when the page is first opened. Code splitting reduces the initial JavaScript size and lightens the load on the main thread.

However, don’t split the bundle into hundreds of tiny files without first measuring the impact. Each additional file creates a network request and a dependency. A good configuration groups code by routes, large components, and real-world user scenarios.

6. Help tree shaking remove dead code

Your package may contain functions that no page calls.

Tree shaking analyzes imports and exports and removes unused parts of modules. This mechanism works particularly well with ES modules. CommonJS has a more dynamic structure, so it’s harder for the bundler to determine which code it can safely remove.

Import specific functions:

import { debounce } from “./utils.js”;

Don’t import the entire library when you only need one method:

import * as utils from “./utils.js”;

Also check the `sideEffects` field in `package.json`. An incorrect value may cause the bundler to leave unnecessary code or, conversely, remove important side effects.

7. Check the final build, not the source code

A well-written TypeScript file says nothing about what the user will actually receive.

Open the production bundle and look for telltale signs of excessive transformation:

_regeneratorRuntime

_objectSpread

_classCallCheck

A single helper won’t cause a catastrophe. But dozens of repetitions across different chunks are already cause for concern.

Chrome DevTools Coverage shows which parts of the JavaScript a page doesn’t use during loading. Lighthouse and the Performance panel help you identify expensive scripts, long-running tasks, and unnecessary legacy transformations.

A bundle analyzer for your bundler will also reveal large libraries, duplicate dependencies, and unexpected polyfills.

We recommend checking at least three metrics:

  • JavaScript size after minification;
  • analysis and execution time;
  • amount of unused code at startup.

The bundle size may decrease only slightly, but execution time can improve significantly. That’s why a single metric doesn’t provide the full picture.

8. Set a performance budget

The team will quickly end up with excess code when a project lacks clear constraints.

Add a check for the size of the initial JavaScript bundle to your CI. Also monitor Total Blocking Time, Interaction to Next Paint, and long-running tasks. Large scripts can cause long operations on the main thread and delay the page’s response to user actions.

Your budget might look like this:

  • Initial JavaScript: no more than 180 KiB after gzip
  • Legacy transforms: no more than 5 KiB
  • One initial chunk: no more than 80 KiB

These numbers are not universal. Choose them after measuring your product, your audience’s devices, and the complexity of the interface.

The main benefit doesn’t come from a specific limit. The benefit comes from automatically responding to regressions.

Don’t blindly remove old code

The fastest package is worthless if some customers can’t place an order or submit a form. Before changing your target browsers, we recommend reviewing your analytics, business requirements, and critical scenarios. After release, monitor JavaScript errors, conversion rates, and actual performance on lower-end devices.

You can also keep a separate legacy bundle for a small portion of your audience. Modern users will get a lightweight build, while older browsers will retain access to core features. This compromise works better than a single heavy file for everyone.

Conclusion

Legacy JavaScript rarely arises from a single problematic feature. It’s created by outdated build targets, universal polyfills, unnecessary dependencies, and the habit of supporting browsers without verifying the actual audience.

You can start with one step: open your production build and see which browsers it’s built for. Then configure Browserslist, reduce transformations, separate the legacy bundle, review polyfills, and split the code by use cases.

We’re not telling you to give up on compatibility. We’re suggesting you make it targeted. Let older browsers get the support they need. Let modern browsers get modern JavaScript.

Share

Was this article helpful to you?

VPS popular offers

sale

-20.6%

CPU
CPU
6 Xeon Cores
RAM
RAM
8GB
Space
Space
100GB SSD
Bandwidth
Bandwidth
500GB
KVM-SSD 8192 HK Linux

€ 59 /mo

€
/mo

Billed annually

sale

-20%

CPU
CPU
6 Epyc Cores
RAM
RAM
8 GB
Space
Space
100 GB NVMe
Bandwidth
Bandwidth
Unlimited
KVM-NVMe 8192 Linux

€ 26.35 /mo

€
/mo

Billed monthly

sale

-19.9%

CPU
CPU
2 Xeon Cores
RAM
RAM
512 MB
Space
Space
10 GB SSD
Bandwidth
Bandwidth
1 TB
KVM-SSD 512 Metered Linux

€ 5.33 /mo

€
/mo

Billed semiannually

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
300 Gb
KVM-HDD HK 4096 Linux

€ 12.84 /mo

€
/mo

Billed monthly

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
60 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 2048 Linux

€ 7.7 /mo

€
/mo

Billed monthly

sale

-20%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 1024 Linux

€ 6.4 /mo

€
/mo

Billed quarterly

sale

-21%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
8 TB
wKVM-SSD 8192 Metered Windows

€ 65 /mo

€
/mo

Billed annually

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
wKVM-SSD 4096 Windows

€ 18.65 /mo

€
/mo

Billed monthly

-10%

CPU
CPU
6 Epyc Cores
RAM
RAM
16 GB
Space
Space
150 GB NVMe
Bandwidth
Bandwidth
Unlimited
Keitaro KVM 16384
OS
CentOS
Software
Software
Keitaro

€ 55.54 /mo

€
/mo

Billed annually

sale

-20%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-wKVM-SSD 16384 Windows

€ 237 /mo

€
/mo

Billed monthly

Other articles on this topic

Setting up a reverse proxy
How to configure VPS
Setting up a reverse proxy
How to configure VPS
cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.