admin管理员组文章数量:1125951
In order to duplicate an array in JavaScript: Which of the following is faster to use?
Slice
method
var dup_array = original_array.slice();
For
loop
for(var i = 0, len = original_array.length; i < len; ++i)
dup_array[i] = original_array[i];
I know both ways do only a shallow copy: if original_array
contains references to objects, objects won't be cloned, but only the references will be copied, and therefore both arrays will have references to the same objects.
But this is not the point of this question.
I'm asking only about speed.
In order to duplicate an array in JavaScript: Which of the following is faster to use?
Slice
method
var dup_array = original_array.slice();
For
loop
for(var i = 0, len = original_array.length; i < len; ++i)
dup_array[i] = original_array[i];
I know both ways do only a shallow copy: if original_array
contains references to objects, objects won't be cloned, but only the references will be copied, and therefore both arrays will have references to the same objects.
But this is not the point of this question.
I'm asking only about speed.
Share Improve this question edited Jun 26, 2021 at 5:06 AbhishekGowda28 1,05410 silver badges19 bronze badges asked Oct 20, 2010 at 13:43 Marco DemaioMarco Demaio 34.4k33 gold badges130 silver badges161 bronze badges 3 |27 Answers
Reset to default 916There are at least 6 (!) ways to clone an array:
- loop
slice
Array.from()
concat
- spread syntax (FASTEST)
- map
A.map(function(e){return e;});
There has been a huuuge BENCHMARKS thread, providing following information:
for blink browsers
slice()
is the fastest method,concat()
is a bit slower, andwhile loop
is 2.4x slower.for other browsers
while loop
is the fastest method, since those browsers don't have internal optimizations forslice
andconcat
.
This remains true in Jul 2016.
Below are simple scripts that you can copy-paste into your browser's console and run several times to see the picture. They output milliseconds, lower is better.
while loop
n = 1000*1000;
start = + new Date();
a = Array(n);
b = Array(n);
i = a.length;
while(i--) b[i] = a[i];
console.log(new Date() - start);
slice
n = 1000*1000;
start = + new Date();
a = Array(n);
b = a.slice();
console.log(new Date() - start);
Please note that these methods will clone the Array object itself, array contents however are copied by reference and are not deep cloned.
origAr == clonedArr //returns false
origAr[0] == clonedArr[0] //returns true
Technically slice
is the fastest way. However, it is even faster if you add the 0
begin index.
myArray.slice(0);
is faster than
myArray.slice();
https://jsben.ch/F0SZ3
what about es6 way?
arr2 = [...arr1];
Easiest way to deep clone Array or Object:
var dup_array = JSON.parse(JSON.stringify(original_array))
本文标签: Fastest way to duplicate an array in JavaScriptslice vs 39for39 loopStack Overflow
版权声明:本文标题:Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736670243a1946881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const getInitialArray = () => {return [[1, 2], [3, 4]}
– rybo111 Commented Jun 7, 2023 at 20:33