Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is concat faster or slower than push

For this code I want to know in javascript what is the best approach?

var output = foo +";"+bar;

or

var output = new Array(foo,bar).join(";");
like image 259
Christophe Debove Avatar asked Dec 13 '12 13:12

Christophe Debove


1 Answers

It doesn't really matter.

There were blogs promoting the first one or the second one, depending on their benchmarks.

But the truth is that javascript engines are heavily optimized and changing, so you won't find a big reproducible and cross-browser difference.

Choose the most readable. Generally it's the first one.

If you really do a loop with 10000 times this push, benchmark it on your customer browsers in your real code, and choose the best but only if there is a significative difference. Don't forget that javascript is fast.

like image 136
Denys Séguret Avatar answered Oct 12 '22 00:10

Denys Séguret