Javascript数组复制,concat vs slice,哪一个更好?
问题描述:
有两种不同的方法来复制数组,使用Array.concat或Array.slice,
there are two different ways to copy a array, using Array.concat or Array.slice,
例如:
var a = [1, 2, 3],
c1 = [].concat(a),
c2 = a.slice(0);
哪种方式更好?
答
为清楚起见,您应该使用记录方法获取数组副本(或其中一部分)的方法:
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.