For-In Loop vs Using Block

Posted by Kirby Turner on February 20, 2015

Brent Simmons talks about for-in loops in a recent post. In it he says:

most of the time the for-in enumeration is the straightforward and less clever approach. (“Less clever” is a good thing.)

I agree with Brent. I’m a fan of the for-in syntax. It’s straightforward and easy to read. But I have to admit in recent years I’ve been using enumerateObjects​UsingBlock: more often than for-in loops. I’ve been favoring the block syntax because something bbum said on Stack Overflow a while back.

enumerateObjectsUsingBlock: will be as fast or faster than fast enumeration (for(... in ...) uses the NSFastEnumeration support to implement enumeration). Fast enumeration requires translation from an internal representation to the representation for fast enumeration. There is overhead therein. Block-based enumeration allows the collection class to enumerate contents as quickly as the fastest traversal of the native storage format. Likely irrelevant for arrays, but it can be a huge difference for dictionaries.

For those who don’t know who bbum is, he’s Bill Bumgarner. He works at Apple and knows a thing or two about Cocoa. So I believe him when he says “enumerateObjectsUsingBlock: will be as fast or faster than fast enumeration.”


Posted in programming. Tagged in ios, os x.


Related Articles