-
-
Save doggy8088/f0b69baedb1349fc782e58d0a24909e9 to your computer and use it in GitHub Desktop.
比較程序編程與函數編程的效能差異
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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