Skip to content
BENIBIX Learn

Polyfill

Development🌐 Web DevelopmentBuild Tools

Also known as Shim, Polyfills

Code that adds a new feature to an old browser that does not have it.

Think of it like

Think of it like a ramp added to an old building. The building gets a feature it was never built with.

Example

Lucy's app uses a new array method. A polyfill adds it for the few customers still on a browser from 2019.

Why it matters

You can use new features and still serve people on old browsers.

Definition

A polyfill is code that adds a new feature to an old browser. It is for browsers that lack it. It first checks whether the feature exists. If it does not, it adds its own version, written with features the old browser does have. Your app can then use the new feature everywhere. Polyfills add size, so add only the ones your users need.

Example code

javascript
if (!Array.prototype.at) {
  Array.prototype.at = function (i) {
    return this[i < 0 ? this.length + i : i];
  };
}
Add the at method only if the browser lacks it.

Common mistake

Sending polyfills for things all browsers have. That adds weight for no gain.

Key takeaways

  1. 1
    Check first. Only fill what is missing.
  2. 2
    Same feature everywhere. Old browsers catch up.
  3. 3
    Costs size. Ship only what users need.

Compare