Home > front end >  Add elements to an array
Add elements to an array

Time:09-23

The function testTime () {
Let num=1 e8;

The console. Time ();
let arr=[];
For (the let I=0; i Arr. Push (I);
}
The console. The log (arr. Length)
The console. TimeEnd ();

The console. Time ();
Let arr2=new Array (num);
For (the let I=0; i Arr2 [I]=I;
}
The console. The log (arr2. Length)
The console. TimeEnd ();

The console. Time ();
Let arr3={};
For (the let I=0; i Arr3 [I]=I;
}
The console. TimeEnd ();
}
Why are arr2 before 8 of 10 square is the shortest, 8 to 10 arr2 when power takes the longest instead,
  • Related