3

Is there any advantage or disadvantage between doing

var a = b.toString()

or

var a = b+""

from performance wise? Both works and serve the same purpose. We can think of doing it on for example a billion of strings.

garenyondem
  • 541
  • 9
  • 24
  • 4
    The `+` operation will implicitly involve a call to `.toString()` anyway. – Pointy Feb 18 '16 at 16:10
  • @Pointy thanks. that was the kind of answer i was looking for. Dont know why my question is marked as dublicate anyway... – garenyondem Feb 18 '16 at 16:14
  • 1
    It really depends a lot on the browser. I did some quick benchmarking at https://jsfiddle.net/kfzxprq1/1/ and https://jsfiddle.net/kfzxprq1/2/. When I run these benchmarks in Firefox `b.toString()` is about 10x faster, whereas `b+""` about 10x faster when I run the same benchmarks in Chrome. – John Slegers Feb 18 '16 at 16:19
  • 1
    And the winner is (*pause for effect*): **`.toString()`**: https://jsperf.com/35486596 – Kijewski Feb 18 '16 at 16:23

0 Answers0