3

Why is this formatted string template

var test = `Today is $ { 
    now
}.`;

faster than just a regular string template on 1 line like

var test = `Today is ${now}.`;

You can see the benchmark in the JsPerf test linked below.

Also why does Firefox perform way better than Chrome on this benchmark?

JsPerf test

SemperMemento
  • 414
  • 2
  • 5
  • 14
  • Getting same results on Opera, strange – Stephan Bijzitter Jan 21 '16 at 13:57
  • 4
    Because the first one doesn't have to do any interpolation on the `now` variable. The first one will print `Today is $ { now }`. The second will print `Today is Thu Jan 21 2016 ...` – CodingIntrigue Jan 21 '16 at 14:00
  • Yes, you're right. The JsPerf beautify function added the whitespaces and I didn't realise it would break the interpolation. I just ran the tests, saw the results and was confused. Thank you for the insight. – SemperMemento Jan 21 '16 at 14:08
  • Uh, in the test you linked the single-line template is faster than multi-line one, not the other way round? – Bergi Jan 21 '16 at 16:12
  • This is [a totally meaningless micro-benchmark](http://mrale.ph/blog/2012/12/15/microbenchmarks-fairy-tale.html). Don't believe anything. – Bergi Jan 21 '16 at 16:16
  • Possible duplicate of [Are ES6 template literals faster than string concatenation?](http://stackoverflow.com/questions/29055518/are-es6-template-literals-faster-than-string-concatenation) – Paul Sweatte Feb 23 '16 at 18:38

0 Answers0