IsEqual: and isKindOfClass: - Which is faster?

For various reasons, to keep the array indices consistent with other things, I have [NSNull null]

inside the array. Like this:

NSArray *arr = @[obj1, obj2, obj3, [NSNull null], obj4];

      

There are two methods I consider when using when iterating through an array to make sure I ignore the value null

, but I'm not sure which is faster.

Method 1

for (id obj in arr) {

    if (![[NSNull null] isEqual:obj]) {

        //Do stiff
    }
}

      

Method 2

for (id obj in arr) {

    if ([obj isKindOfClass:[MyObject class]]) {

        //Do stiff
    }
}

      

My question is, since I am iterating over this array in order to properly handle the viewed scrolling (so it is executed many times when the user scrolls and it is critical that it runs as fast as possible) , which of these methods is faster?

+3


source to share


1 answer


[NSNull null]

is a one-liner, so it is easiest to check if the object pointer is the same.

If you really want to be fast, do the following:

for (id obj in arr) {
    if ([NSNull null]!=obj) {
        //Do stuff
    }
}  

      



BUT it is unlikely that you will see ANY difference for the visual interface as we are talking about a very small difference.

The option discussed in the comments is to put [NSNull null]

in a local variable to speed up checks, but it's possible the compiler will do that for you, so I'll just put that here for your reference:

NSObject *null_obj=[NSNull null];
for (id obj in arr) {
    if (null_obj!=obj) {
        //Do stuff
    }
}

      

+1


source







All Articles