Hello! Tell me, how can you copy an array into JavaScript so that, when you change the copy, the original does not change? For example:
a = [0, 1, 2, 3] b = clone(a) b[0] = 10 a[0] == 0 // => true
Is it possible to implement such a clone
method?
Hello! Tell me, how can you copy an array into JavaScript so that, when you change the copy, the original does not change? For example:
a = [0, 1, 2, 3] b = clone(a) b[0] = 10 a[0] == 0 // => true
Is it possible to implement such a clone
method?
var a = [0, 1, 2, 3] var b = a.slice(0); b[0] = 10; console.log('a: ' + a); console.log('b: ' + b);
slice - returns a shallow copy of a portion of an array to a new array object.
In this case, from beginning to end
Technically, slice
is the quickest path, HOWEVER it will be even faster if you add 0
- as the beginning of the "cutting"
myArray.slice(0);
faster than
myArray.slice();
.... so speak the languages%)
var a = [0, 1, 2, 3] var b = [].concat(a) b[0] = 10 console.log(a + '\n' + b)
var oldArray = [0, 1, 2, 3]; var newArray = oldArray.slice();
In chrome, slice(0)
really a bit faster than just slice()
and 2 times faster than concat
. But Array.from
even concat
u loses 10 times - 3 seconds per million iterations o_O.
But Edge does not think so - there concat
is the fastest. And Array.from
is still 10 (with a tail) times slower than concat
a - 1.4 seconds.
When you run the snippet, the browser will hang for a few seconds - this is normal.
function test(f) { var a = [0, 1, 2, 3]; var t = performance.now(); f(a); t = performance.now() - t; console.log((""+f).match(/var b = .*$/m)[0] + " // " + t.toFixed(3) + " ms"); } test(function testSlice(a) { for (var q=0; q<1000000; ++q) { var b = a.slice(); } }); test(function testSlice0(a) { for (var q=0; q<1000000; ++q) { var b = a.slice(0); } }); test(function testConcat(a) { for (var q=0; q<1000000; ++q) { var b = [].concat(a); } }); test(function testFrom(a) { for (var q=0; q<1000000; ++q) { var b = Array.from(a); } }); test(function testAssign(a) { // Так делать не надо, просто для сравнения for (var q=0; q<1000000; ++q) { var b = Object.assign([], a); } }); test(function testDesruct(a) { for (var q=0; q<1000000; ++q) { var b = [...a]; } });
Source: https://ru.stackoverflow.com/questions/550976/
All Articles