Loops

JavaScript performance comparison

Test case created by cferdinandi

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var wizards = [
  	{
  		name: 'Harry Potter',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Cedric Diggory',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Tonks',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Ronald Weasley',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Hermione Granger',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Harry Potter',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Cedric Diggory',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Tonks',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Ronald Weasley',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Hermione Granger',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Harry Potter',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Cedric Diggory',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Tonks',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Ronald Weasley',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Hermione Granger',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Harry Potter',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Cedric Diggory',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Tonks',
  		house: 'Hufflepuff'
  	},
  	{
  		name: 'Ronald Weasley',
  		house: 'Gryfindor'
  	},
  	{
  		name: 'Hermione Granger',
  		house: 'Gryfindor'
  	}
  ];
  
  var wizardsNew = [];

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for
for (var i = 0; i < wizards.length; i++) {
	console.log(wizards[i].name);
}
pending…
forEach()
wizards.forEach(function (wizard) {
	console.log(wizard.name);
});
pending…
for map
for (var i = 0; i < wizards.length; i++) {
	wizardsNew.push(wizards[i].name);
}
pending…
forEach() map
wizards.forEach(function (wizard) {
	wizardsNew.push(wizard.name);
});
pending…
map()
wizardsNew = wizards.map(function (wizard) {
	return wizard.name;
});
pending…
for filter
for (var i = 0; i < wizards.length; i++) {
	if (wizards[i].house === 'Hufflepuff') {
		wizardsNew.push(wizards[i]);
	}
}
pending…
forEach() filter
wizards.forEach(function (wizard) {
	if (wizard.house === 'Hufflepuff') {
		wizardsNew.push(wizard);
	}
});
pending…
filter()
wizardsNew = wizards.filter(function (wizard) {
	return wizard.house === 'Hufflepuff';
});
pending…
for combo
for (var i = 0; i < wizards.length; i++) {
	if (wizards[i].house === 'Hufflepuff') {
		wizardsNew.push(wizards[i].name);
	}
}
pending…
forEach() combo
wizards.forEach(function (wizard) {
	if (wizard.house === 'Hufflepuff') {
		wizardsNew.push(wizard.name);
	}
});
pending…
filter() + map()
wizardsNew = wizards.filter(function (wizard) {
	return wizard.house === 'Hufflepuff';
}).map(function (wizard) {
	return wizard.name;
});
pending…

Compare results of other browsers

Revisions

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

0 Comments