Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:48:33+00:00 2026-06-09T20:48:33+00:00

When I use NSMutableArray or NSMutableDictionary , if I know the number of elements

  • 0

When I use NSMutableArray or NSMutableDictionary, if I know the number of elements I want to put in or the max number of elements, I usually create them with arrayWithCapacity or dictionaryWithCapacity, but I wonder does it really help to specify an (initial) capacity for the array/dictionary?

I don’t know how it is implemented internally, but I believe it is possible that when the number of elements in a collection hits the capacity or even approaches the capacity, the collection may extend its capacity, so if I created a mutable array with capacity 32, as long as I put the 32nd object in it, it will expand itself to another capacity? Or even if I put the 30st object in it, its capacity will be expanded as it thinks there will be more objects?

So if these methods really help, should I use something like:

 *withCapacity:maxNumberOfElements * 1.5

instead of

*withCapacity:maxNumberOfElements

so it will have more than enough capacity for my objects and won’t expand when I put in all the objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:48:34+00:00Added an answer on June 9, 2026 at 8:48 pm

    This is useful when you are planning to populate a collection with a large number of elements, or when you know the exact number of elements that you are going to load. Resizing a collection takes CPU cycles, so resizing unnecessarily ultimately translates into reducing the time your device can operate on a battery.

    Consider this example: let’s say you are about to load 3000 elements into an array. If you allocate the default array with space for, say, 16 items, the array will need to resize eight times before arriving at the size necessary to hold 3000 elements. The elements copied in the initial positions will need to be copied each time the array is resized, resulting in 3000+ additional copy operations. When you know the exact number of elements, you can prevent the copying from happening.

    In addition, your array will not waste memory for elements that you are not going to add: if you add 3000 elements one by one, the array may grow to 4000 internally in anticipation of more elements; the last 1000 elements will be wasted.

    To summarize, you should initialize your collections with capacity when you know the exact target size for sure. This situation comes up often when you deserialize data from a file or a network connection. In situations when you do not know the size, it is better not to make guesses, and let the default initialization run its course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store two NSMutableArray that I use as global array in AppDelegate.
I'm trying to use ABRecordRef within an NSMutableArray, but it doesn't seem to work.
I want update position of Particles for store this information I use NSMutableArray. I
I'm new to objective-C. I want to use NSMutableArray to store some objects i
I use a loop to load images into an NSMutableArray . Each iteration loads
I am experiencing memory leaks linked to NSMutableArray's in a project configured to use
use C#,want to upload excel file on google doc. bellow syntax use to upload
I am trying to create a NSMutableArray by reading in a .txt file and
I have an id<NSFastEnumeration> object. I want to count the elements inside the object.
Continuing off this post: Performance hit incurred using NSMutableDictionary vs. NSMutableArray> I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.