Skip to content

Instantly share code, notes, and snippets.

@doggy8088

doggy8088/f01.js Secret

Last active October 23, 2018 09:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doggy8088/f0b69baedb1349fc782e58d0a24909e9 to your computer and use it in GitHub Desktop.
Save doggy8088/f0b69baedb1349fc782e58d0a24909e9 to your computer and use it in GitHub Desktop.
比較程序編程與函數編程的效能差異
var i, j, people1, people2, person, sum = 0, people = [
{
"name": {
"first": "Will",
"last": "Huang"
},
"company": "MINIASP"
},
{
"name": {
"first": "James",
"last": "Huang"
},
"company": "Coolrare"
},
{
"name": {
"first": "Jeff",
"last": "Wu"
},
"company": "MINIASP"
}
]
console.time('init');
for(i=0; i < 1000000; i++) {
people1 = [];
for(j=0; j<people.length;j++) {
var person = people[j];
if(person.name.last === 'Huang') { people1.push(person); }
}
people2 = []
for(j=0; j<people1.length;j++) {
person = people1[j];
people2.push({
name: person.name.first + ' ' + person.name.last,
company: person.company
});
}
for(j=0; j<people2.length;j++) {
person = people2[j];
sum = sum + person.company.length;
}
}
console.timeEnd('init');
var i, people = [
{
"name": {
"first": "Will",
"last": "Huang"
},
"company": "MINIASP"
},
{
"name": {
"first": "James",
"last": "Huang"
},
"company": "Coolrare"
},
{
"name": {
"first": "Jeff",
"last": "Wu"
},
"company": "MINIASP"
}
]
console.time('init');
for(i=0; i < 1000000; i++) {
people
.filter(function(person) {
return person.name.last === 'Huang';
})
.map(function(person) {
return {
name: person.name.first + ' ' + person.name.last,
company: person.company
};
})
.reduce(function(sum, person) {
return sum + person.company.length;
}, 0);
}
console.timeEnd('init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment