Skip to content

Javascript Array – join() vs toString() method

28 June 2014

Javascript Array.toString()  overrides the method inherited from Object to represent array contents as a string. For the purpose the method joins array elements comma separated. Starting from ECMAScript 5 (JavaScript 1.8.5) toString() method can be applied to any object. When toString() is executed it calls the join() method if exists, or Object.prototype.toString() otherwise.

With that knowledge here is a reasonable question: when using arrays which one is better to use?

It turns out that Array.toString() applied on a string array serves the best.

Here is the (jsperf) test case with interesting results. Not only for the method but also for browser’s engines used – 70 ops/sec for IE11 vs almost 35 ops/sec for Firefox. Bellow are mine.

1_Chrome 2_Opera 3_IE11 4_Firefox

From → JavaScript

Leave a Comment

Leave a comment