# NSUserDefaults Performance Boost

DevFeed: [NSUserDefaults Performance Boost](<https://devfeed.tech/articles/nsuserdefaults-performance-boost-31889.md>)

Original publisher: [Read original article](<http://engineering.flipboard.com//2015/03/nsuserdefaults-performance>)

Author: https://twitter.com/timonus (Tim Johnsen)

Published: 2015-03-16T00:00:00Z

Content type: tutorial

Language: en

Sources: [Flipboard](<https://devfeed.tech/sources/flipboard.md>)

Topics: [iOS](<https://devfeed.tech/topics/ios.md>), [Caching](<https://devfeed.tech/topics/caching.md>), [Open Source](<https://devfeed.tech/topics/open-source.md>), [Extension](<https://devfeed.tech/topics/extension.md>), [GitHub](<https://devfeed.tech/topics/github.md>)

Tags: [apple](<https://devfeed.tech/tags/apple.md>), [cache](<https://devfeed.tech/tags/cache.md>), [extensions](<https://devfeed.tech/tags/extensions.md>), [github](<https://devfeed.tech/tags/github.md>), [ios](<https://devfeed.tech/tags/ios.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article explains that iOS 8 simulator performance problems were linked to synchronous disk I/O while emulating cfprefsd. It describes an in-memory write-through cache for NSUserDefaults that reduced the reported time spent in CFPreferences from 80% to 1%, with possible side effects when debugging extensions.

## Source excerpt

Since iOS 8 was released we've noticed some sluggishness when using Flipboard in the simulator. When taking a trace with Instruments in normal use we noticed a significant amount of time was being spent in 1 CFPreferences. On Twitter, an Apple engineer acknowledged that there were some changes in iOS 8 that added something called 1 cfprefsd, and that emulating that in the simulator required synchronous reads from disk. This seemed to be the bottleneck we were encountering. @timonus unfortunately cfprefsd doesn't currently exist in the simulator, and emulating its behavior requires synchronize disk IO -- David Smith (@Catfish_Man) January 21, 2015 @timonus it's on my list to fix, but it's a large effort and device took priority -- David Smith (@Catfish_Man) January 21, 2015 About a month ago we were talking as a team about how slow we felt our app had become to debug in the simulator, so we decided to try to do something to speed it up. Our approach was to introduce a man-in-the-middle write-through cache to 1 NSUserDefaults in memory. We do so by swizzling out all of 1 NSUserDefaults' setters and getters and adding a per-instance 1 NSMutableDictionary to cache values. We avoid compiling this for device builds using 1 TARGET_IPHONE_SIMULATOR because there aren't such performance issues on device. The increase in performance is dramatic, where we were once spending 80% of our time in 1 CFPreferences we're now spending 1%. If you're seeing performance issues related to 1 NSUserDefaults in the simulator I recommend trying this out, it's open source and available for download on GitHub. To get started using it all you need to do is include it in your project. Please note that use of this category may cause side effects when debugging extensions. This is documented in the GitHub project.