为了在JavaScript中复制数组:下面哪个使用更快?

片的方法

var dup_array = original_array.slice();

For循环

for(var i = 0, len = original_array.length; i < len; ++i)
   dup_array[i] = original_array[i];

我知道这两种方法都只做一个浅拷贝:如果original_array包含对对象的引用,对象将不会被克隆,但只有引用将被复制,因此两个数组将有对相同对象的引用。 但这不是问题的关键。

我只是在问速度。


当前回答

ECMAScript 2015的扩展操作符方式:

基本的例子:

var copyOfOldArray = [...oldArray]
var twoArraysBecomeOne = [...firstArray, ..seccondArray]

在浏览器控制台中尝试:

var oldArray = [1, 2, 3]
var copyOfOldArray = [...oldArray]
console.log(oldArray)
console.log(copyOfOldArray)

var firstArray = [5, 6, 7]
var seccondArray = ["a", "b", "c"]
var twoArraysBecomOne = [...firstArray, ...seccondArray]
console.log(twoArraysBecomOne);

参考文献

展开运算符的6大用途 传播的语法

其他回答

一个简单的解决方案:

original = [1,2,3]
cloned = original.map(x=>x)

基准时间!

function log(data) { document.getElementById("log").textContent += data + "\n"; } benchmark = (() => { time_function = function(ms, f, num) { var z = 0; var t = new Date().getTime(); for (z = 0; ((new Date().getTime() - t) < ms); z++) f(num); return (z) } function clone1(arr) { return arr.slice(0); } function clone2(arr) { return [...arr] } function clone3(arr) { return [].concat(arr); } Array.prototype.clone = function() { return this.map(e => Array.isArray(e) ? e.clone() : e); }; function clone4(arr) { return arr.clone(); } function benchmark() { function compare(a, b) { if (a[1] > b[1]) { return -1; } if (a[1] < b[1]) { return 1; } return 0; } funcs = [clone1, clone2, clone3, clone4]; results = []; funcs.forEach((ff) => { console.log("Benchmarking: " + ff.name); var s = time_function(2500, ff, Array(1024)); results.push([ff, s]); console.log("Score: " + s); }) return results.sort(compare); } return benchmark; })() log("Starting benchmark...\n"); res = benchmark(); console.log("Winner: " + res[0][0].name + " !!!"); count = 1; res.forEach((r) => { log((count++) + ". " + r[0].name + " score: " + Math.floor(10000 * r[1] / res[0][1]) / 100 + ((count == 2) ? "% *winner*" : "% speed of winner.") + " (" + Math.round(r[1] * 100) / 100 + ")"); }); log("\nWinner code:\n"); log(res[0][0].toString()); <textarea rows="50" cols="80" style="font-size: 16; resize:none; border: none;" id="log"></textarea>

自单击按钮以来,基准测试将运行10秒。

我的结果:

Chrome (V8引擎):

1. clone1 score: 100% *winner* (4110764)
2. clone3 score: 74.32% speed of winner. (3055225)
3. clone2 score: 30.75% speed of winner. (1264182)
4. clone4 score: 21.96% speed of winner. (902929)

Firefox (SpiderMonkey Engine):

1. clone1 score: 100% *winner* (8448353)
2. clone3 score: 16.44% speed of winner. (1389241)
3. clone4 score: 5.69% speed of winner. (481162)
4. clone2 score: 2.27% speed of winner. (192433)

赢家代码:

function clone1(arr) {
    return arr.slice(0);
}

赢家引擎: SpiderMonkey (Mozilla Firefox)

如果你是关于切片,它是用来从数组复制元素,并创建一个克隆相同的编号。或者更少的元素。的元素。

Var arr = [1,2,3,4,5]; 函数slc() { Var切片= arr。片(0 5); / /加勒比海盗。切片(开始复制主数组的位置,没有。新数组中的项) console.log(切片); } slc (arr);

我整理了一个简短的演示:http://jsbin.com/agugo3/edit

我在Internet Explorer 8上的结果是156、782和750,这表明切片在这种情况下要快得多。

那我们的方式呢?

arr2 = [...arr1];