Map, for, spread javascript benchmark for copy array

copy for: 2508.226ms
copy map: 1188.982ms
copy spread: 1898.890ms
const map = [
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 10],
  [11, 12, 13, 14, 15]
];
const n  = 10e6;

console.time('copy for');
for (let i = 0; i < n; i++) {
  const m = [];
  for (let row = 0; row < map.length; row++) {
    m[row] = [];
    for (let reel = 0; reel < map[row].length; reel++) {
      m[row][reel] = map[row][reel];
    }
  }
}
console.timeEnd('copy for');

console.time('copy map');
for (let i = 0; i < n; i++) {
  const m = map.map(a => a.map(item => item));
}
console.timeEnd('copy map');

console.time('copy spread');
for (let i = 0; i < n; i++) {
  const m = map.map(a => [...a]);
}
console.timeEnd('copy spread');
13 августа 2019, 13:29