{"id":41,"date":"2016-01-14T11:57:56","date_gmt":"2016-01-14T17:57:56","guid":{"rendered":"https:\/\/chibicode.org\/?p=41"},"modified":"2016-01-15T11:53:43","modified_gmt":"2016-01-15T17:53:43","slug":"just-how-expensive-is-creating-an-nsdateformatter","status":"publish","type":"post","link":"https:\/\/chibicode.org\/?p=41","title":{"rendered":"Just how expensive is creating an NSDateFormatter?"},"content":{"rendered":"<p>One of the many mantras that is drilled into iOS devs is that you shouldn&#8217;t recreate expensive, complex objects like formatters every time you need them. Instead, keep them around in a static or instance variable and use them as needed. To test this, I created a playground and put this file in the Sources folder:<\/p>\n<p>[gist https:\/\/gist.github.com\/JoshuaSullivan\/6c0d0134e66c60c6cd8c file=&#8221;FormatterTest.swift&#8221;]<\/p>\n<p>I didn&#8217;t put the code directly into the playground because doing ANYTHING in a playground 100k times is going to take a very long time and not be very representative of real-world conditions.<\/p>\n<p>Here&#8217;s the playground code, which simply invokes the tests and records the results:<\/p>\n<p>[gist https:\/\/gist.github.com\/JoshuaSullivan\/6c0d0134e66c60c6cd8c file=&#8221;Playground.swift&#8221;]<\/p>\n<p>The results were unequivocal: creating an NSDateFormatter each time you need to use it is roughly <em>9x slower<\/em> than creating it once and using it repeatedly. That said, this is only likely to be an issue for tasks where there is a lot of date formatting going on, such as in a table view with dates in every cell. If you have a situation where you only need to format a single date infrequently (today&#8217;s date in a page header, for example), then you shouldn&#8217;t worry about hanging on to the date formatter; even though it&#8217;s heavy, it only takes a tiny fraction of a second to instantiate one.<\/p>\n<h3>CONVENTIONAL WISDOM: CONFIRMED<\/h3>\n<p><strong>Addendum:<\/strong> At the request of a coworker (@rexeisen), I added in a test of the static <code>NSDateFormatter.<br \/>\nlocalizedStringFromDate(_:dateStyle:timeStyle:)<\/code> test. As you can see, the results are no better than creating an instance each time. That said, it would be more convenient to use for 1-off formatting tasks, where keeping the formatter around is unnecessary.<\/p>\n<p><strong>Addendum II (2016-01-15):<\/strong> Further testing has revealed that changing the timeStyle and dateStyle of an <code>NSDateFormatter<\/code> is tremendously expensive. Even more so than just creating a new formatter for each use! Across several trials, performance using a single <code>NSDateFormatter<\/code> that is re-parameterized on each use was 10% slower than creating a new formatter each time and a full 10x slower than using a single, pre-configured formatter. The take away here is, create a formatter for each repeating case, don&#8217;t try to make a single shared formatter do all the work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the many mantras that is drilled into iOS devs is that you shouldn&#8217;t recreate expensive, complex objects like formatters every time you need them. Instead, keep them around in a static or instance variable and use them as needed. To test this, I created a playground and put this file in the Sources &hellip; <a href=\"https:\/\/chibicode.org\/?p=41\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Just how expensive is creating an NSDateFormatter?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[17,16,6],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-code","tag-nsdateformatter","tag-performance","tag-swift"],"_links":{"self":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":7,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}