Has anybody done benchmarking, or can link to an article on this subject? Particularly interested in IE results, since usually JS performance is not a problem in other browsers.
I would like to know how much slower it is to do something like:
var numbers = [1, 2, 3, 4, 5, 6, 7];
var results = numbers.map(function() {
// do some stuff
});
instead of the typical:
var numbers = [1, 2, 3, 4, 5, 6, 7];
var results = [];
for (var i = 0; i < numbers.length; i++) {
var number = numbers[i];
var result;
// do some stuff
results.push(result);
}
I obviously prefer the functional style, but I assume the extra overhead of calling an extra function for each item could slow things down with big collections.
Thanks!
Not content with the lack of proof on this subject, I wrote a short benchmark. It’s far from perfect but I think it answers the question.
I ran it in IE 8/win, and while the functional method is slower, it’s never going to be the bottleneck in real code. (Unless you’re doing stuff that you shouldn’t be doing in the client anyway)
So I will be using the cleaner approach whenever I have to pick (yay)
(Best of 5)
Functional method: 453ms
Old school approach: 156ms