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

Advertisements

From → JavaScript

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: