green-code-initiative / ecoCode-javascript

Reduce the environmental footprint of your JS/TS software programs
https://ecocode.io
GNU General Public License v3.0
18 stars 17 forks source link

JS - Avoid backticks usage without interpolation needs #28

Open JulienWilhelm opened 11 months ago

JulienWilhelm commented 11 months ago

Is your feature request related to a problem? Please describe.

In JS, we can write strings inside :

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.

utarwyn commented 11 months ago

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:

JulienWilhelm commented 10 months ago

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.