Array Shallow Copy

Benchmark created on


Setup

var arrayToCopy = `Extended kindness trifling remember he confined outlived if. Assistance sentiments yet unpleasing say. Open they an busy they my such high. An active dinner wishes at unable hardly no talked on. Immediate him her resolving his favourite. Wished denote abroad at branch at.

An country demesne message it. Bachelor domestic extended doubtful as concerns at. Morning prudent removal an letters by. On could my in order never it. Or excited certain sixteen it to parties colonel. Depending conveying direction has led immediate. Law gate her well bed life feet seen rent. On nature or no except it sussex.`.split('');
var shallowCopy = [];

Test runner

Ready to run.

Testing in
TestOps/sec
Array Concat
shallowCopy = arrayToCopy.concat();
ready
Array Slice
shallowCopy = arrayToCopy.slice();
ready
For Loop
for (let i = 0; i < arrayToCopy.length; ++i)
{
  shallowCopy.push(arrayToCopy[i]);
}
ready
For Loop With Declared Length
var arrayLength = arrayToCopy.length;
for (let i = 0; i < arrayLength; ++i)
{
  shallowCopy.push(arrayToCopy[i]);
}
ready
While Loop
let i = arrayToCopy.length - 1;
while (i--)
{
  shallowCopy.push(arrayToCopy[i]);
}
ready
Spread Operator
shallowCopy = [...arrayToCopy];
ready
Slice With Begin
shallowCopy = arrayToCopy.slice(0);
ready
Slice With Begin and End
shallowCopy = arrayToCopy.slice(0, arrayToCopy.length);
ready
For Of Assignment
let i = 0;
for (shallowCopy[i++] of arrayToCopy);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.