3 ms·
If performance matters a manual for loop will be faster. `arr.map()` isn't all that fast (except in the recent Firefoxes).
by susi22 9y ago
If performance matters a manual for loop will be faster. `arr.map()` isn't all that fast (except in the recent Firefoxes).
- masklinn 9y agoI get similar performances for the "naive" map: let trimmed = strings.map(str => str.trim()); and a c-style for let trimmed = []; for(var i=0; i<strings.length; ++i) { trimmed.push(strings[i].trim()); } in both Chrome (62) and Firefox (58). The c-style for is very lightly faster in FF (not Chrome) but it's 767 ops/s to 725 ops/s so not exactly brain-breaking (chrome is at 454 and 452). Safari (11) is the only one where map is a fair bit slower than c-style (420 to 377). In all cases, both the clever map and for..of are slower than the naive map, though the magnitude varies Chrome yields very little difference but Firefox and Safari dislike for..of and like "clever map" even less.
- jostylr 9y agoI added the floor loop to the code and made a console based webpage for it: http://jostylr.com/cleverdumb.html http://jostylr.com/cleverdumb.html Kind of hackish, but you can open it up in a browser and look at the console for the run times. Basically, the clever code is generally worse, and the arrow versus for loop in Firefox and Safari are similar. But in Chrome, the for loop is quicker; it was far more noticeable in node than in chrome. One quirk of chrome is that the first time the for loop is run, it seems to take longer than subsequent runs and I am not sure why that is. I tried moving around initialization stuff and it did not seem to make any differences. Times from node: Prototype: 105.467ms Arrow function: 106.674ms For: 98.820ms Prototype: 105.743ms Arrow function: 106.192ms For: 82.733ms Prototype: 105.512ms Arrow function: 106.517ms For: 82.625ms Basically, code in the comfortable style. I used to love for loops, but have now switched over to map and forEach when it makes sense to do so.