Loops JS

Benchmark created on


Preparation HTML


Setup

class Plane {
  constructor() {
    this.element = document.createElement('div');
    this.id = Math.random();

    document.body.appendChild(this.element);

    this.element.addEventListener('click', this.death);
  }

  death() {
    this.element.classList.add('expl');
    console.log('Avion détruit !');
  }
  
  getId(){
  	return this.id;
  }
}

let planes = [];
const plane1 = new Plane();
const plane2 = new Plane();
const plane3 = new Plane();

planes.push(plane1);
planes.push(plane2);
planes.push(plane3);

Test runner

Ready to run.

Testing in
TestOps/sec
Boucle for...of
for(const plane of planes){
	console.log(plane.getId());
};
ready
Boucle for classique
const length = planes.length;

for(let i = 0; i < length; i++){
	console.log(planes[i].getId());
}
ready
Méthode forEach()
planes.forEach(function(plane){
	console.log(plane.getId());
});
ready
Méthode map()
const ids = planes.map(plane => plane.getId());
console.log(ids);
ready
Méthode forEach() bis
planes.forEach(plane => {
    console.log(plane.getId());
});
ready
Boucle for...in
for (const index in planes) {
    console.log(planes[index].getId());
}
ready

Revisions

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