Babel compiles backtick template literals to regular quotes for browser compatibility, but this does not necessarily improve JavaScript performance.
Abstract
The article discusses the role of Babel in compiling ES2015 backtick template literals into regular quotes to ensure compatibility with older browsers like Internet Explorer. It explains that while Babel can convert these modern JavaScript features, performance tests using jsPerf indicate that string concatenation and variable interpolation with backticks have similar execution speeds in modern browsers. The author concludes that transpiling backtick literals with Babel has a negligible impact on performance, emphasizing that the primary benefit of Babel is increased browser compatibility rather than performance enhancement.
Opinions
The author suggests that backtick literals are more readable than traditional string concatenation methods.
There is a preference for using new ECMAScript features like template literals, facilitated by tools like Babel.
The author implies that measurement errors in performance testing can be significant and should be considered when interpreting results.
The author expresses enthusiasm for adopting new JavaScript features, indicating a strong interest in the evolution of the language.
There is a sentiment that the performance of string concatenation methods is comparable across modern browsers, making the choice between them more about syntax preference and compatibility rather than speed.
Does Babel Compile Backticks (``) to Quotes and Does That Make JavaScript Code Faster?
Babel will compile the ES2015 feature backtick literals using the transform-template-literals plugin
Babel is a JavaScript tool that performs compilation (or transpilation), allowing developers to use the new ECMAScript features before they are broadly adopted by browsers or in order to support older browsers.
It is like a translation tool that translates JavaScript into ancient Greek.
Or ancient JavaScript, as the case may be.
Put another way, Babel converts next-gen JavaScript to browser-compatible JavaScript. And since backticks are a next-gen feature added in ES6, Babel can compile them to regular quotes using a free plugin.
Of course, this is not an entirely scientific test, for the reason that actually supporting IE would require polyfills, and polyfills are probably slower than modern JavaScript without polyfills, depending on the implementations.
Supporting older browsers using compilation and polyfills almost certainly results in slower code — or at least somewhat longer load times.
That is because polyfills are reimplementations of the new features, so they are more likely to be slower than compilation, which is like a translation.
That means that even if Babel helps performance regarding string concatenation, other polyfills could be slowing down the codebase.
And I did not even mention how slow those browsers’ implementations of JavaScript were 10+ years ago compared to modern JavaScript.
Now onto the results regarding Babel and transform-template-literals.
One thing to note is unlike my last speed test on JavaScript backticks, jsPerf decided to run this set in Firefox instead of Chrome. This is currently not a configurable setting through their awesome test suite.
But the results are basically the same: Any type of string concatenation runs at about the same speed in JavaScript, whether or not backticks are involved, and whether or not the + operator or concat() method is used.
So I should not expect Babel transpiling my backtick literals to quote strings to improve performance, though of course increased compatibility and not improved performance is the entire justification for using Babel at all.
Measurement Error Rules Everything Around Me
The cool thing about backtick literals is they really seem to be just as fast as any other quote literal in a modern JavaScript browser.
In fact, rerunning my recent simple tests, except on Firefox instead of Chrome, shows that interpolation is probably not slower at all, at least not enough to be worth optimizing for:
Phew, after celebrating backtick literals in my recent article, I was worried that Babel was gonna make me look bad with a big advantage!
Actually, transpiling backtick literals probably makes no difference at all to the performance of your JavaScript code. One less thing to worry about.
To me, this just reinforces the idea that we should have open arms to adopt new features, especially because we have tools like Babel for automatically making our code more browser-compatible.
Also, I probably have a crush on backtick literals. But is that a bad thing?