4 ms·
The coercing rules are fairly predictable, but oftentimes incomprehensible until you understand what's happening behind the scenes. It's one of the biggest wea
by warfangle 13y ago
The coercing rules are fairly predictable, but oftentimes incomprehensible until you understand what's happening behind the scenes.
It's one of the biggest weaknesses of JavaScript. Much bigger than callback hell, garbage collection cycles etc.
My preferred style uses Array.prototype.join('') for string concatenation, non-coercive equality operations, and only using the + operator when doing math. It's a little cumbersome, but avoids ambiguity upon reading (and in the case of string concatenation, can be faster).
- ricardobeat 13y ago> and in the case of string concatenation, can be faster myth: http://jsperf.com/array-join-vs-string-connect/37 http://jsperf.com/array-join-vs-string-connect/37
- FreeFull 13y agoIn Firefox 25 on Linux, Array Join Nocopy seems to perform the fastest.
- warfangle 13y agoExactly. I rarely build up an array within a loop and then join it. That kind of thing just seems dirty - use a buffer! It's usually, e.g., ['some string ', someVar, ' some rest of string'].join('');
- warfangle 13y agoIn some cases, nocopy join is faster. Thus. "Can be" faster. But most people do string concatenation in situations where performance of the concatenation really doesn't quite matter so much. I optimize for readability, understandability, and prevention of stupid silly mistakes first. Performance comes later.