I have a method to build and return an array:
- (NSArray *)foo
{
NSUInteger capacity = [self getArrayCapacity];
if (capacity == 0) {
return @[];
} else {
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:capacity];
// add elements to the array, as many as capacity
...
return array;
}
}
Is there a difference in memory used or performance if I simplify the code as follows:
- (NSArray *)fooSimplified
{
NSUInteger capacity = [self getCapacity];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:capacity];
// add elements to the array, as many as capacity
...
return array;
}
}
So when capacity == 0
instead of returning @[]
it would return [[NSMutableArray alloc] initWithCapacity:0]
Is there a performance or memory penalty/difference?