What is a polyfill?

A polyfill is code that implements a modern language feature or web API in environments that do not support it natively, so you can use the standard feature everywhere; it only defines the feature if it is missing.

Polyfill vs transpiler

  • A transpiler (Babel, SWC, TypeScript, esbuild) rewrites syntax the target cannot parse (optional chaining, class fields) into older syntax.
  • A polyfill adds missing functions and objects at runtime (Array.prototype.at, Object.groupBy, structuredClone, fetch). Many features need both: async/await syntax is transpiled, and older targets also need a Promise polyfill.

How to write one safely

  1. Feature-detect first: only define it if it is missing.
  2. Follow the spec behaviour closely, including edge cases (negative indexes, NaN, holes).
  3. Define methods as non-enumerable with Object.defineProperty, like built-ins, so they do not show up in for...in.
  4. Keep this and argument handling correct.

Using polyfills in practice

  • core-js (usually through Babel's useBuiltIns: "usage" or SWC) adds only the polyfills your code and browser targets need.
  • Define targets with browserslist.
  • Modern frameworks (Next.js) include the polyfills they need for supported browsers.
  • Avoid loading big polyfill bundles for every user; modern browsers rarely need them.

Ponyfills

A ponyfill provides the same functionality without patching globals (you import a function). This is safer for libraries, because it never conflicts with the host page.

Code example

js
1// Polyfill Array.prototype.at (ES2022) following the spec, only if missing
2if (!Array.prototype.myAt) {
3 Object.defineProperty(Array.prototype, "myAt", {
4 value: function myAt(index) {
5 const len = this.length >>> 0;
6 let i = Math.trunc(Number(index)) || 0;
7 if (i < 0) i += len;
8 return i < 0 || i >= len ? undefined : this[i];
9 },
10 writable: true,
11 configurable: true,
12 enumerable: false, // built-ins are non-enumerable
13 });
14}
15const arr = [10, 20, 30];
16console.log(arr.myAt(0), arr.myAt(-1), arr.myAt(5), arr.myAt("1")); // 10 30 undefined 20
17for (const key in arr) if (key === "myAt") console.log("would leak into for...in"); // nothing logged
18
19// Polyfill Object.groupBy (ES2024) with feature detection
20const groupBy =
21 Object.groupBy ??
22 function groupBy(items, keyFn) {
23 const out = Object.create(null);
24 let i = 0;
25 for (const item of items) (out[keyFn(item, i++)] ??= []).push(item);
26 return out;
27 };
28console.log(groupBy([1, 2, 3, 4], (n) => (n % 2 ? "odd" : "even"))); // { odd: [1, 3], even: [2, 4] }
29
30// Ponyfill style: export a function instead of patching globals
31const structuredCloneSafe = globalThis.structuredClone ?? ((v) => JSON.parse(JSON.stringify(v)));
32console.log(structuredCloneSafe({ a: [1] })); // { a: [1] }
33
34delete Array.prototype.myAt; // clean up the demo

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app