Use core-js per-feature imports to control bundle size and prototype mutation
Replace global core-js stable imports with targeted per-feature imports to shrink bundles and avoid mutating shared built-in prototypes.
01 Nov 2025, 05:49 UTC

The problem hiding in your entry file
Many frontend applications start with a global polyfill import:
import 'core-js/stable';
import './app';
That one line asks core-js v3 to polyfill every finished ECMAScript feature it ships. It also patches JavaScript’s built-in prototypes—Array.prototype, Promise, Object, and others—so any code in the same realm can use methods such as Array.prototype.flat without importing anything.
A realm is the JavaScript environment that owns the global object and its built-ins. When you patch a prototype in that realm, every script loaded afterward sees the change, including third-party libraries that never asked for it.
If your users run modern browsers, much of that global polyfill is unnecessary code. The useful takeaway is to import only the features you need, preferably as standalone implementations that do not touch built-ins at all.
Global stable imports versus modular actual imports
core-js/stable is a convenience entry point. It pulls in the full set of finished features and mutates globals once. That is simple, but it is blunt: you pay for features your target browsers already support, and you alter the shared environment.
core-js v3 also provides modular paths such as core-js/actual/array/flat-map. These are actual exports: standalone functions or values that you import directly, without patching Array.prototype. There is also core-js/proposals for stage proposals, but those specifications can change, so they require extra care.
The distinction matters for libraries and applications alike. A library that imports core-js/actual/array/flat-map receives a function it can call without risking side effects for its consumers. An application that still needs arr.flatMap() syntax can either update call sites to use the imported function or import a targeted stable polyfill for just that method instead of the whole stable set.
Worked example: replace the global import
Before, in your application entry file:
import 'core-js/stable';
import './app';
After, import only the features your code actually uses:
import flatMap from 'core-js/actual/array/flat-map';
import './app';
If ./app calls items.flatMap(...) directly, the standalone import above will not satisfy that call because Array.prototype was never patched. You have two options: change the call site to flatMap(items, ...), or add a targeted global polyfill for just that method instead of the whole stable set.
Pin the major version in package.json because core-js major versions introduce breaking changes:
"core-js": "3"
Run npm ls core-js in your project root to confirm the installed version. This is a read-only command and requires no special permissions.
Check the result before you ship
- Search your source for global imports:
grep -R "core-js/stable" src --include="*.js" --include="*.ts". This is read-only. - Run your production build, for example
npm run build, and compare the compressed JavaScript asset sizes before and after the change. Look in your build output directory, such asdist/orbuild/. - Load a test module that contains only the targeted import and confirm the imported binding is a function. If you used an
actualimport, verify thatArray.prototype.flatMapremains unchanged in a clean realm unless you explicitly assign it.
If the build succeeds but runtime errors appear, a feature you removed may have been relied upon implicitly. Restore the previous import lines, rebuild, and audit which APIs your code actually calls.
Trade-offs and limitations
- Modular imports do not tree-shake themselves; your bundler must eliminate unused code, and behavior varies by bundler and configuration.
- Mixing
core-js/stablewith per-feature imports can duplicate code and create inconsistent behavior if versions mismatch. core-js/proposalstracks specifications that are not final, so a later proposal revision can break your polyfill.- Standalone
actualimports require you to update call sites that depend on patched prototypes.
Closing
Start by running npm ls core-js and searching for core-js/stable in your source. Replace the global entry point with targeted imports for the features your browsers lack, build, compare bundle sizes, and verify prototype behavior in isolation. You will ship less JavaScript, and your built-ins will stay under your control.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.