There are probably many ways to significantly improve performance, but in order to be able to offer any, we really need to know more about what objects in arrays βhaveβ: what do they represent? How are they used? (For example, are items in the storage array displayed as a table?)
NSMutableDictionary , NSMutableSet etc. can be combined with NSMutableArray to efficiently and efficiently implement the model.
For example, let's say we know that an object represents a person: MDPerson . A person has a gender, date of birth, name, unique identifier and a set of attributes that can change. Given this higher understanding of what the object represents, we know that 2 people are equal only if their unique identifiers are the same (in other words, 2 different people can have the same name, gender and date of birth). Say your main NSMutableArray consists of a list of 3,000 people. The incoming array consists of 500 people who are already in the main NSMutableArray . Some of these 500 people may have βupdatedβ attributes, which means that their instance in the main array must be updated with this information.
Given this understanding, it is clear that the main list should be implemented as an NSMutableDictionary , not an NSMutableArray . In the dictionary, the only key will be the unique identifier of the person, and his copy for the person will be the value for the key. Then you can scroll through an incoming array of 500 people only once:
// main dictionary is called personIDsAndPersons for (MDPerson *person in incomingPersons) { MDPerson *existingPerson = [personIDsAndPersons objectForKey:[person uniqueID]]; // if nil, the person doesn't exist if (existingPerson) { // update the existing person attributes [existingPerson setUniqueAttributes:[person uniqueAttributes]]; } }
Again, without knowing more details or a higher level of understanding of objects, we really shoot in the dark.
You mentioned that 2 elements are the same if they have the same name. Does this mean that each element in the main array of 3000 objects has a unique name? If so, you can use NSMutableDictionary to provide access to objects in an efficient way, having keys in the dictionary as a name and values ββas an instance of the object. Then you can use a separate NSMutableArray , which is used only for display purposes: it allows an ordered, sorted organization of the same objects that are stored in NSMutableDictionary . Remember that when you add an object to an array or a dictionary, usually you do not create a new copy, you just save the existing object.