The difference in performance between [] + [] and [] .join (',') + []. Join (',')

While I was messing around with arrays I found this.

why there is a significant difference between the following three statements:

[1,2]+[3,4];

[1,2].toString()+[3,4].toString();

[1,2].join(',')+[3,4].join(',');

      

enter image description here And if it [1,2]+[1,2]

does the same thing as converting to a string, and then concatenating two strings, then there shouldn't be their performance in somewhat similar

This question is inspired by this answer

+3


source to share


1 answer


It probably has something to do with implicit and explicit coercion.

For the [1,2]+[3,4]

interpreter to figure out what string

is the desired result.

For [1,2].toString()+[3,4].toString();

and, [1,2].join(',')+[3,4].join(',');

you are already telling the interpreter that it works with strings.



The difference between the last two lines is pretty minor.

After all, even if you do it a million times, you won't really notice the difference.

+4


source







All Articles