Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
Chrome 87
Windows
Desktop
4 years ago
Test name Executions per second
While Loop with declaration 291005.3 Ops/sec
While Loop with Slice 1496097.1 Ops/sec
While Loop with Push and Slice 1413510.1 Ops/sec
While Loop with Push 287501.0 Ops/sec
Array.from with map 227292.5 Ops/sec
Array.from with map and slice 665677.4 Ops/sec
While Loop with Slice and Internal Declaration 298060.1 Ops/sec
Script Preparation code:
AخA
 
var size = 15;
Tests:
  • While Loop with declaration

    x
     
    const array = [];
    array.length = size;
    let index = array.length;
    while (index-- > 0) {
        const temp = [];
        temp.length = size;
        array[index] = temp;
    }
  • While Loop with Slice

     
    const array = [];
    array.length = size;
    let index = array.length;
    const temp = [];
    temp.length = size;
    while (index-- > 0) array[index] = temp.slice(0);
  • While Loop with Push and Slice

     
    const array = [];
    array.length = size;
    let index = array.length;
    const temp = [];
    temp.length = size;
    while (index-- > 0) array.push(temp.slice(0));
  • While Loop with Push

     
    const array = [];
    array.length = size;
    let index = size;
    while (index-- > 0) {
        const temp = [];
        temp.length = size;
        array.push(temp);
    }
  • Array.from with map

     
    const array = Array.from(Array(size), () => {
      const temp = [];
      temp.length = size;
      return temp;
    });
  • Array.from with map and slice

     
    const temp = [];
    temp.length = size;
    const array = Array.from(temp, () => temp.slice());
  • While Loop with Slice and Internal Declaration

     
    const array = [];
    array.length = size;
    let index = array.length;
    while (index-- > 0) {
      const temp = [];
      temp.length = size;
      array[index] = temp;
    }