I was reading the AirBnB JavaScript Style Guide recently and came across:
"Symbols and BigInts cannot be faithfully polyfilled, so they should not be used when targeting browsers/environments that don’t support them natively."
Since I didn't know what polyfilling was, I couldn't understand the full implication of the rest of the sentence.
Wikipedia provided a helpful definition:
"In web development, a polyfill is code that implements a feature on web browsers that do not support the feature."
Ok, so that was polyfilling in theory -- using code to patch discrepancies between browsers. But what about in practice? And why doesn't it work for Symbols and BigInts? I decided to hunt down a good example.
I found a great one on Javascript.info. It explained that the Math.trunc() function doesn't exist in all JS engines, so the polyfill code for it checks for the built-in func, and if it doesn't exist, executes the same functionality.
I found the exact reason why polyfilling is not a wise idea for BigInt on JavaScript.info as well:
"To emulate such behavior, a polyfill would need to analyze the code and replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance."
But what about Symbols? I found the definition of the Symbol type on MDN:
"Symbol is a built-in object whose constructor returns a symbol primitive — also called a Symbol value or just a Symbol — that’s guaranteed to be unique."
Because these values must be guaranteed to be unique, I can imagine the process to generate them with a polyfill must also be high performance, like that of generating BigInts. That's my surmise, but I was unable to find anything that supported it, so I'd appreciate input here!
The last question I have is:
Why does the AirBnB style guide say Symbols and BigInts cannot be faithfully polyfilled? It seems that they can't be frugally polyfilled, in terms of performance, but I wonder why they used the adjective "faithfully". Does it mean that polyfilling these types changes them in some way?
Top comments (2)
They probably mean it can’t be faithfully polyfilled because the polyfill can’t reproduce all of the functionality expected. There may be parts of it that can’t be polyfilled, so instead of shimming it, they are shammed. Just a guess as I haven’t dug into the polyfills for Symbols and BigInt.
For some context:
Can I just say that I love that JS devs have different definitions for the words "shim" and "sham". This looks like a reasonable answer to me!