ecoCode-javascript
ecoCode-javascript copied to clipboard
JS - Avoid backticks usage without interpolation needs
Is your feature request related to a problem? Please describe.
In JS, we can write strings inside :
- Single quotes ;
- Double quotes ;
- Backticks.
By using backticks, we can inject variables that will be evaluated on runtime, just like that :
console.log(`this is my ${variable} content.`);
I made a simple jsperf test to evaluate the performance between each approach.
<!-- HTML -->
<p id="my-para"></p>
// JS. Test 1.
document.getElementById('my-para').textContent = 'A few words...';
// JS. Test 2.
document.getElementById('my-para').textContent = "A few words...";
// JS. Test 3.
document.getElementById('my-para').textContent = `A few words...`;
Here are the results :
- Using double quotes is always fastest ;
- Using backticks is always slowest.
Describe the solution you'd like
A dedicated rule? Backticks without interpolation needs should be avoid.
Hello, thank you for the rule idea!
Unfortunately, when I try to reproduce on jsperf (a very good tool, by the way!), the results are a bit random and the backticks aren't always the slowest.
I think it needs a little more work to validate its relevance. Any help is welcome on this topic :+1:
Hum. I made these tests again today, and I got various results too! It seems I was wrong (even if it should be logic)!
Do not close the issue so fast; I will try to make some others tests.
Running -yet another- benchmark, with https://jsbench.me/, shows that there is no performance gain using quotes instead of backticks (and even shows sometimes backticks could be faster).
Furthermore, as mentionned in #86, if the performance is not impacted this is simply a styling issue (that already has rules within eslint/prettier).
OK. So it appears that this is not so relevant after all.