Reputation: 6973
In JavaScript is the former or the latter more efficient? Or is there even a difference?
// Method one
var path = first_part + '/' + second_part + '/' + third_part;
// Method two
var path = [first_part, second_part, third_part].join('/');
Beyond the savings of a whopping two characters there's no visual reason to prefer one over the other. But I'm curious to know if in most JavaScript interpreters one is faster or more efficient than the other and if so, is it significantly so?
Upvotes: 0
Views: 120
Reputation: 2301
Using the concatenation operator is faster than using join()
:
See:
See also:
Upvotes: 1
Reputation: 6588
The second method is more efficient in terms of maintenance if you ever need to change the delimiter.
Upvotes: 2