ecoCode-javascript icon indicating copy to clipboard operation
ecoCode-javascript copied to clipboard

JS - Avoid backticks usage without interpolation needs

Open JulienWilhelm opened this issue 2 years ago • 2 comments
trafficstars

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 :

  1. Using double quotes is always fastest ;
  2. Using backticks is always slowest.

Describe the solution you'd like

A dedicated rule? Backticks without interpolation needs should be avoid.

JulienWilhelm avatar Oct 10 '23 12:10 JulienWilhelm

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.

image

I think it needs a little more work to validate its relevance. Any help is welcome on this topic :+1:

utarwyn avatar Oct 19 '23 11:10 utarwyn

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.

JulienWilhelm avatar Nov 08 '23 16:11 JulienWilhelm

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).

Image

Furthermore, as mentionned in #86, if the performance is not impacted this is simply a styling issue (that already has rules within eslint/prettier).

mathis-girault avatar May 21 '25 12:05 mathis-girault

OK. So it appears that this is not so relevant after all.

JulienWilhelm avatar May 22 '25 06:05 JulienWilhelm