Template Literals, All the Way!

2017-02-23

Today, while at work, I had to turn a single-quoted string into a template literal because mid-way through the string I realized I had to put in an apostrophe. This has happened often enough that I want to stop thinking in this dual single-quoted/template-literal mode and just go template-literal all the time.

Now before I say anything more I’d like to clarify that I’m working with NodeJS almost all the time with full feature support for template strings and no issues with things like minification. So anything in my research that had to do with not supported in all environments was discarded as invalid.

After an initial bout of googling I didn’t find much to deter me from using template literals.

This SO answer speaks of (among other things) IDE support and how having it makes working with template literals easier coz placeholders get highlighted. The Airbnb Style Guide prefers single-quoted strings and using template literals for interpolation. It doesn’t say we shouldn’t use template literals all the time. And then there’s this thread on reddit that leads to a bunch of different places, most of which talk about browser support and minification and stuff that doesn’t matter in my controlled NodeJs environment.

This guy ran tests to figure out if using template literals poorly impacts performance but according to the results template literals are actually faster.

So I decided to see if there was any other research around performance impact. One would assume that template literals would be slower than a plain old string (when not interpolating) because a plain old string is just a plain old string but a template literal would check for interpolation even when no placeholders exist (though a good compiler would account for this and optimize accordingly).

Someone did a test of ES6 features on a bunch of platforms (including Node v6.9.3) and the results don’t tell me anything about single quoted strings vs. template literals in a non-interpolating string (in general the results don’t look very promising for ES6 features, though; eek!).

Most research seems to be centered around whether it’s more performant to concatenate strings or use template literals when interpolation is required. No one seems to have a strong stance on whether template literals should or should not be used for everything.

Some arguments I came across for why they shouldn’t be used all the time (and my rebuttal):

  • What if you need to have a ` or ${} in your code? I cannot recall a single instance in my nearly three years of coding in NodeJs that I’ve had to use these in my strings. I doubt I’ll come across more than a few such cases in my entire lifetime. And if I have to, I can always escape them #BackslashFTW.
  • My keyboard has an European layout and using those backticks is a pain in the buttocks! I can sympathize with this. If your tools go against you, there is little you can do. But in my personal case (and the case of my team), we don’t have this issue. Hence, another point that I need not take into consideration (though you definitely should if you have such a keyboard).

So it doesn’t look like there is much compelling research around why I shouldn’t be using those oh-so-handy template literals. How about you guys find some for me and let me know?

And if you think I got anything else in this post wrong, let me know about that too.


More posts like this

My ES Lint and Why

2018-09-21 | #coding-style #eslint #nodejs

I recently updated my company’s main NodeJS repository to include formatting via prettier-eslint and linting via eslint. My colleague asked me to explain my choice of rules since it’s tedious to go through each and every rule and figure out why it was chosen and I thought it was worth getting a blog post out of it, so here we go. This is the eslint config I plan to use:

Continue reading 


Semico what?

2016-12-21 | #coding-style #javascript #nodejs

Recently, at my workplace, a colleague felt a rustle up his jimmies due to our lack of semicolon usage in our NodeJs repos. The number of projects without semicolons were in the tens and there really wasn’t a good way to introduce semicolons in them even had the team agreed to do so. But that got me thinking. Do we really need semi-colons? Why have them or not have them? What are the arguments for and against the issue?

Continue reading 