Search code examples
ioscocoa-touch

Why use NSArray instead of NSOrderedSet


In many good articles online, including this one: http://nshipster.com/nsorderedset/ it is stated that we should still prefer NSArray over NSOrderedSet in general.

An advantage that i've noticed is the syntactic sugar of NSArray: @[obj1, obj2, ...]

So what are the other advantages of NSArray that makes it the preferred collection manager instead of NSOrderedSet, which seems to have a lot of benefits (fast access to elements), and it basically includes the NSArray functionality?


Solution

  • I timed inserting 1,000,000 random NSNumbers and found that NSMutableOrderedSet was about 7 to 10 times slower. With fewer items the times became closer, at 1000 items the times were about 2.5 times slower. Access times were close.

    These times represent about the best for NSMutableOrderedSet that I saw:

    iterations: 1000000

    NSMutableOrderedSet: 782 msec
    count: 999868

    NSMutableArray: 112 msec
    count: 1000000

    Note that there were 132 duplicates that were rejecter by NSMutableOrderedSet.