views:

86

answers:

1

Hi, I'm a big fan of using the forEach method on nodeLists like this:

var nodes = document.querySelectorAll(".foo");

[].forEach.call(nodes, function (item) {
    //do stuff with item
});

I was wondering though, does doing it that way take longer than the regular way? e.g.

for(var i=0;i<nodes.length;i++){
    //do stuff with nodes[i];
}
+4  A: 

Here's a nice performance comparison. According to it Array.forEach is slower than a native for loop.

Darin Dimitrov
Cheers, thanks for the link. That's exactly what I was after.BTW, the jQuery.each speed is a bit surprising.
Yansky