Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active January 3, 2020 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/435b4ef9bc2bd0b6fe982d99b633e077 to your computer and use it in GitHub Desktop.
Save cferdinandi/435b4ef9bc2bd0b6fe982d99b633e077 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Loop Performance</title>
<style type="text/css">
body {
margin: 1em auto;
max-width: 40em;
text-align: center;
width: 88%;
}
</style>
</head>
<body>
<script>
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;
//
// Test Runner
//
var runTest = function (name, test) {
wizardsNew = [];
console.time(name);
for (var i = 0; i < 100000; i++) {
test();
}
console.timeEnd(name);
};
//
// Tests
//
runTest('for map', function () {
for (var i = 0; i < wizards.length; i++) {
wizardsNew.push(wizards[i].name);
}
});
runTest('forEach() map', function () {
wizards.forEach(function (wizard) {
wizardsNew.push(wizard.name);
});
});
runTest('map()', function () {
wizardsNew = wizards.map(function (wizard) {
return wizard.name;
});
});
runTest('for filter', function () {
for (var i = 0; i < wizards.length; i++) {
if (wizards[i].house === 'Hufflepuff') {
wizardsNew.push(wizards[i]);
}
}
});
runTest('forEach() filter', function () {
wizards.forEach(function (wizard) {
if (wizard.house === 'Hufflepuff') {
wizardsNew.push(wizard);
}
});
});
runTest('filter()', function () {
wizardsNew = wizards.filter(function (wizard) {
return wizard.house === 'Hufflepuff';
});
});
runTest('for combo', function () {
for (var i = 0; i < wizards.length; i++) {
if (wizards[i].house === 'Hufflepuff') {
wizardsNew.push(wizards[i].name);
}
}
});
runTest('forEach() combo', function () {
wizards.forEach(function (wizard) {
if (wizard.house === 'Hufflepuff') {
wizardsNew.push(wizard.name);
}
});
});
runTest('filter() + map()', function () {
wizardsNew = wizards.filter(function (wizard) {
return wizard.house === 'Hufflepuff';
}).map(function (wizard) {
return wizard.name;
});
});
runTest('reduce()', function () {
wizardsNew = wizards.reduce(function (arr, wizard) {
if (wizard.house === 'Hufflepuff') {
arr.push(wizard.name);
}
return arr;
}, []);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment