Both object iterations mentioned are slow. Use native for loop
swanidhi opened this issue · 3 comments
// bad
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
console.log(prop);
}
// bad
Object.keys(obj).forEach(prop => console.log(prop));
//good -- fastest
keys = Object.keys(obj);
for (var i = 0, l = keys.length; i < l; i++) {
result = result + obj[keys[i]];
}
Hi @swanidhi, did you read the guidelines?
JavaScript
Performance
Favor readability, correctness and expressiveness over performance.
and forEach variation is most readable at most cases;
Agreed. However native for loop is not very bad to look at either. And the
performance difference is considerable. Hence suggested.
Sent from my Nexus 5
On Aug 10, 2015 12:54 PM, "misbeliever" notifications@github.com wrote:
Hi @swanidhi https://github.com/swanidhi, did you read the guidelines?
JavaScript PerformanceFavor readability, correctness and expressiveness over performance.
and forEach variation is most readable at most cases;
—
Reply to this email directly or view it on GitHub
#752 (comment)
.
What @misbeliever said.