25 iOS App Performance Tips & Tricks

This article gathers together 25 tips and tricks that you can use to improve the performance of your apps, in the form of a handy checklist. By .

4.8 (6) · 1 Review

Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Advanced Performance Tips

Looking for some elite ways to become a total code ninja? These advanced performance tips can be used when appropriate to make your apps run as efficiently as possible!

 
22) Speed up Launch Time

Launching your app quickly is very important, especially when the user launches for the first time. First impressions mean a lot for an app!

The biggest thing that you can do to ensure your app starts as quickly as possible is to perform as many asynchronous tasks as possible, such as network requests, database access, or parsing data.

As well, try to avoid fat XIBs, since they’re loaded on the main thread. But recall that storyboards don’t have this problem — so use them if you can!

Note: The watchdog doesn’t run while debugging with Xcode, so be sure to test your app with your device disconnected from Xcode while testing for launch performance.

 
23) Use Autorelease Pool

NSAutoreleasePool is responsible for releasing the autoreleased objects within a block. Usually, it’s called automatically by UIKit. But there are some scenarios where may you need to create NSAutoreleasePools manually.

For example, if you create a lot of temporary objects in your code, you’ll note that memory usage increases until these objects are released. The problem is that this memory is only released after UIKit drains its autorelease pool, which means this memory is held much longer than necessary.

The good news is that you can avoid this by creating these temporary objects inside your own @autoreleasepool block, as shown in the code below:

NSArray *urls = <# An array of file URLs #>;
for (NSURL *url in urls) {
    @autoreleasepool {
        NSError *error;
        NSString *fileContents = [NSString stringWithContentsOfURL:url
                                         encoding:NSUTF8StringEncoding error:&error];
        /* Process the string, creating and autoreleasing more objects. */
    }
}

This releases all the autorelease objects at the end of each iteration.

You can read more about NSAutoreleasePool in Apple’s official documentation.

 
24) Cache Images – Or Not

There are two common ways to load a UIImage from an app bundle. The first, and more common way to do it is using imageNamed. The second, and less common way, is with imageWithContentsOfFile.

Why are there two methods which effectively achieve the same thing?

imageNamed has the advantage of caching the image as it’s loaded. The documentation for imageNamed explains it this way:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

Alternately, imageWithContentsOfFile simply loads the image with no caching.

The two methods are demonstrated in the code block below:

UIImage *img = [UIImage imageNamed:@"myImage"]; // caching
// or
UIImage *img = [UIImage imageWithContentsOfFile:@"myImage"]; // no caching

When would you use one over the other?

If you’re loading a large image that will be used only once, there’s no need to cache the image. In this case imageWithContentsOfFile will fit the bill nicely – this way, the OS doesn’t waste memory caching the image.

However, imageNamed is a much better choice for images that you’re going to be reusing in your app. This way, the OS saves time having to constantly load the image from disk.

 
25) Avoid Date Formatters Where Possible

If you have a lot of dates that you need to parse with NSDateFormatter, you need to tread carefully. As mentioned previously, it’s always a good idea to reuse NSDateFormatters whenever possible.

However, if you need more speed, you can parse dates directly using C instead of NSDateFormatter. Sam Soffes wrote a blog post about this topic which presents some code to parse ISO-8601 date strings. However, you can easily tweak his code examples to fit your particular needs.

Well, that sounds great — but would you believe there’s an even better way?

If you can control the format of the dates you are dealing with, choose Unix timestamps if at all possible. Unix timestamps are simply integers which represent how many seconds have passed since the “epoch”, which is simply the common reference date of 00:00:00 UTC on 1 January 1970.

You can easily transform this timestamp into an NSDate, as shown below:


- (NSDate*)dateFromUnixTimestamp:(NSTimeInterval)timestamp {
    return [NSDate dateWithTimeIntervalSince1970:timestamp];
}

This is even faster than the C function!

Note that many web APIs return timestamps as milliseconds, since it’s very common for Javascript to eventually consume and process this data. Just be aware that you’ll need to divide the timestamp by 1000 before passing it to the dateFromUnixTimestamp method.

Where to Go From Here?

The following WWDC videos are highly recommended viewing for anyone interested in optimizing the performance of their apps. You’ll first need to ensure that you’ve registered your Apple ID as a developer, but once that’s done, you can have your fill of any of the videos from WWDC 2012:

  • #406: Adopting Automatic Reference Counting
  • #238: iOS App Performance: Graphics and Animations
  • #242: iOS App Performance: Memory
  • #235: iOS App Performance: Responsiveness
  • #409: Learning Instruments
  • #706: Networking Best Practices
  • #514: OpenGL ES Tools and Techniques
  • #506: Optimizing 2D Graphics and Animation Performance
  • #601: Optimizing Web Content in UIWebViews and Websites on iOS
  • #225: Up and Running: Making a Great Impression with Every Launch

There’s also some videos from WWDC 2011 that contain some great information as well:

  • #308: Blocks and Grand Central Dispatch in Practice
  • #323: Introducing Automatic Reference Counting
  • #312: iOS Performance and Power Optimization with Instruments
  • #105: Polishing Your App: Tips and tricks to improve the responsiveness and performance
  • #121: Understanding UIKit Rendering

There’s even more videos, mostly from iOS 5 Tech Talks:

  • Your iOS App Performance Hitlist
  • Optimizing App Performance with Instruments
  • Understanding iOS View Compositing

Based on “Your iOS App Performance Hitlist” video there’s a post written by Ole Begemann, that is a textual summary about the original presentation by Michael Jurewitz.

Apple has also provided a very useful resource called Performance Tuning, which provides a lot of great additional tips and tricks about making your apps perform well on iOS.

I hope you found this collection of tips helpful. If you know of a good tip that isn’t mention here, or have any comments or questions, please join the forum discussion below!


This is a post by iOS Tutorial Team Member Marcelo Fabri, an iOS developer working at Movile. Check out his personal website or find him on Twitter or on .