In some cases, yes, it's pure sugar.
In some cases, yes, you may notice a speed difference. But only if you're looping through a megaton of items and doing a supermegaton of processing.
In most case, it's effect on performance is not worth mentioning.
The main point of it, though, is it's a nice clean way to iterate a collection AND have a new local scope for each iteration of the loop. The closure provided by .each(fn(item, index)) comes in very handy in many cases.
Plus:
for (var i = 0; i < blah.length; i++) {
var item = blah[i];
doSomething(item);
}
is more verbose than:
blah.each(doSomething);
And finally, you're right that .each is one of the lesser value-add methods in Enumerable. Certainly .invoke, .collect, etc... are doing more work. But personally I like the "predicate and delegate" patterns - also prevalent in C# and Java (via Generics) - that Enumerable provides with it methods, including each.
I've been bitten several times with a for loop that didn't quite work as expected because I, or someone, forgot about the absence of block level scoping in javascript.
--
Ryan Gahl
Manager, Senior Software Engineer
Nth Penguin, LLC
http://www.nthpenguin.com--
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire:
1-920-574-2218Blog:
http://www.someElement.comLinkedIn Profile:
http://www.linkedin.com/in/ryangahl