1

As I saw this question What's the best way to convert a number to a string in JavaScript?, It seems that if there is a var 'a' typeof number, a + '' is the fastest way to convert to String. And I don't know why, because in the last, a has to be called with toString and binary operator shouldn't be checked with the type of both operands? So Here's question

  1. Why is a+'' the fastest way to convert a number to a string?
  2. Is this the fastest way for all types or just numbers?
Community
  • 1
  • 1
김원용
  • 11
  • 2
  • About the fastest way to parse numbers to strings, see [this post](http://stackoverflow.com/questions/5765398/whats-the-best-way-to-convert-a-number-to-a-string-in-javascript) – nicovank Nov 08 '16 at 02:58
  • 2
    The Javascript interpreter doesn't need to call `.toString()`, it can use internal code that performs the string conversion directly. So it saves the function call overhead. – Barmar Nov 08 '16 at 03:06
  • And there's no overhead from the concatenation operator, because it knows that `+ ''` doesn't need to call the concatenation function. – Barmar Nov 08 '16 at 03:06
  • This is an interesting question. I also looked at the ECMA specs and was surprised to find what you said. I think there are two possibilities (incomplete ideas) a) Using the + operator skips some steps in toString method (look at 19.1.3.6 in ECMA - http://www.ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring) b) The lookup for + is much faster than toString() – Netham Nov 08 '16 at 03:33

0 Answers0