Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is keepingCapacity when using array.removeAll more performant?

Tags:

swift

Assuming we have an array

var foo = ["bar", "baz", "qux"]

...and we're going to re-populate that array with the same number of elements via multiple foo.append() calls, after we've removed all elements: is there any performance advantage to keeping the existing capacity of the array?

// Keep the capacity
foo.removeAll(keepingCapacity: true)

vs.

// Don't keep the capacity
foo.removeAll(keepingCapacity: false)
like image 534
2Toad Avatar asked Oct 28 '25 12:10

2Toad


1 Answers

From the docs.

keepCapacity

Pass true to request that the collection avoid releasing its storage. Retaining the collection’s storage can be a useful optimization when you’re planning to grow the collection again. The default value is false.


So the answer is yes i guess.

like image 96
Rakesha Shastri Avatar answered Oct 31 '25 01:10

Rakesha Shastri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!