Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a shim and a polyfill?

Both seem to be used in web development circles, see e.g. HTML5 Cross Browser Polyfills, which says:

So here we're collecting all the shims, fallbacks, and polyfills...

Or, there's the es5-shim project.

In my current project we're using a number of these, and I want to stick them all in the same directory. So, what should I call this directory---shims, or polyfills?

like image 628
Domenic Avatar asked Jul 06 '11 16:07

Domenic


People also ask

What is a shim vs polyfill?

Shims Versus PolyfillsA shim is a library that brings a new API to an older environment, using only the means of that environment. A polyfill is a shim for a browser API. It typically checks if a browser supports an API. If it doesn't, the polyfill installs its own implementation.

What is polyfill used for?

Formally, "a polyfill is a shim for a browser API." Polyfills allow web developers to use an API regardless of whether or not it is supported by a browser, and usually with minimal overhead. Typically they first check if a browser supports an API, and use it if available, otherwise using their own implementation.

Is polyfill necessary?

Newer browsers don't need the polyfill, but typically the polyfill is served to all browsers. This reduces performance in modern browsers in order to improve compatibility with legacy ones. We don't want to make that compromise.

What is a shim node?

Shim is a node. js-based browser-compatibility tool that lets you synchronize several devices/browsers and surf the same pages simultaneously on all of them.


2 Answers

  • A shim is any piece of code that performs interception of an API call and provides a layer of abstraction. It isn't necessarily restricted to a web application or HTML5/CSS3.

  • A polyfill is a type of shim that retrofits legacy browsers with modern HTML5/CSS3 features usually using Javascript or Flash.

Answering your specific question, call your directory shims if you want to keep the directory generic.

like image 90
Arsalan Ahmed Avatar answered Oct 02 '22 21:10

Arsalan Ahmed


Shim

If you are familiar with the adapter pattern, then you know what a shim is. Shims intercept API calls and create an abstract layer between the caller and the target. Typically shims are used for backward compatibility. For instance the es5-shim npm package will let you write ECMAScript 5 (ES5) syntax and not care if the browser is running ES5 or not. Take Date.now as an example. This is a new function in ES5 where the syntax in ES3 would be new Date().getTime(). If you use the es5-shim you can write Date.now and if the browser you’re running in supports ES5 it will just run. However, if the browser is running the ES3 engine es5-shim will intercept the call to Date.now and just return new Date().getTime() instead. This interception is called shimming. The relevant source code from es5-shim looks like this:

if (!Date.now) {     Date.now = function now() {         return new Date().getTime();     }; } 

Polyfill

Polyfilling is really just a specialized version of shimming. Polyfill is about implementing missing features in an API, whereas a shim wouldn’t necessarily be as much about implementing missing features as it is about correcting features. I know these seems overly vague, but where shims are used as a more broader term, polyfill is used to describe shims that provide backward compatibility for older browsers. So while shims are used for covering up old sins, polyfills are used for bringing future enhancements back in time. As an example there is no support for sessionStorage in IE7, but the polyfill in the sessionstorage npm package will add this feature in IE7 (and older) by using techniques like storing data in the name property of the window or by using cookies.

like image 37
Kjetil Klaussen Avatar answered Oct 02 '22 19:10

Kjetil Klaussen