I know cleaner and nicer types, like String(1234)and Number("1234"), but I just tried to compare alternative ways to do the same, in particular "" + 1234 // -> "1234"and - - "1234" // -> 1234.
The results were quite unexpected (for me). I iterated over every path 100,000,000 times in Chrome.
I used this simple code.
var then = Date.now();
for (var i = 0; i < 100000000; ++i) {
var a = - - "1234";
};
console.log(Date.now() - then);
Number("1234")It took 2351 ms, while it - - "1234"took only 748 ms.
Similarly, on the contrary, it String(1234)took 3701 ms, while it "" + 1234took only 893 ms.
The difference is surprisingly huge.
My questions are: what makes explicit casting so slower than implicit casting? My intuition tells me that it should be the other way around.
? - - "1234"? ?
PS: Firefox. 500 ( ). ? - ? , .