33

I'm using an array to store cached objects loaded from a database in my iPhone app, and was wondering: are there any significant disadvantages to using NSMutableArray that I should know of?

edit: I know that NSMutableArray can be modified, but I'm looking for specific reasons (performance, etc..) why one would use NSArray instead. I assume there would be a performance difference, but I have no idea whether it's significant or not.

igul222
  • 8,557
  • 14
  • 52
  • 60

5 Answers5

50

If you're loading objects from a database and you know exactly how many objects you have, you would likely get the best performance from NSMutableArrays arrayWithCapacity: method, and adding objects to it until full, so it allocates all the memory at once if it can.

Behind the scenes, they're secretly the same thing - NSArray and NSMutableArray are both implemented with CFArrays via toll free bridging (a CFMutableArrayRef and a CFArrayRef are typedef's of the same thing, __CFArray *) *

NSArray and NSMutableArray should have the same performance/complexity (access time being O(lg N) at worst and O(1) at best) and the only difference being how much memory the two objects would use - NSArray has a fixed limit, while NSMutableArray can use up as much space as you have free.

The comments in CFArray.h have much more detail about this.

*: As Catfish_Man points out below, this isn't true anymore.

zadr
  • 2,505
  • 18
  • 19
  • 4
    Note that NSArray is no longer implemented in terms of CFArray (though it was four years ago when this answer was written) – Catfish_Man Apr 27 '13 at 22:01
  • Where have you read about performance of data structure in objective C? – George Dec 10 '14 at 07:53
  • 1
    @George The performance of data structures in Objective-C is pretty much the same as it is in other languages. If you want Foundation-specific details, http://ridiculousfish.com/blog/posts/array.html has some insights, although things specific details have changed in the past decade. – zadr Dec 23 '14 at 08:16
25

The performance difference of using NSArray versus NSMutable array arises primarily when you use an API that wants to copy the array. if you send -copy to an immutable array, it just bumps the retain count, but sending -copy to a mutable array will allocate heap memory.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
14

In addition, NSMutableArray is not threadsafe, while NSArray is (same with all the mutable vs. "immutable" objects). This could be a huge problem if you're multithreading.

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
9

The main disadvantage to NSMutableArray is that an object owning a NSMutableArray may have the array changed behind its back if another object also owns it. This may require you to code your object more defensively, less aggressively chasing performance.

If the NSMutableArray is not exposed outside of the object, this isn't a concern.

NSArray is a better choice for sharing, precisely because it is immutable. Every object using it can assume it won't change, and doesn't need to defensively make a copy of it.

This is probably the same reason that NSDictionary copies its keys, rather than simply retaining them: it needs to be sure that they won't mutate, and copying is the only way to guarantee that.

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
2

You can't modify NSArray once it is created. If you need to add/remove objects from your array then you will use NSMutableArray - not much options for that. I assume NSArray is optimized for fixed array operations. Mutable array provides flexibly of being modifiable.

stefanB
  • 77,323
  • 27
  • 116
  • 141