Javascript array copying, concat vs slice, which one is better? [closed]

there are two different ways to copy a array, using Array.concat or Array.slice,

for example:

var a = [1, 2, 3],

    c1 = [].concat(a),

    c2 = a.slice(0);

which way is better?

Answers:

Answer

For clarity, you should use the method that is the documented method of taking a copy of an array (or portion thereof):

var c2 = a.slice(0);

On any decent browser the performance difference will be negligible, so clarity should be the deciding factor.

Answer

In terms of performance, the difference between the two options are minimal, according to a jsperf.com test.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.