Clicky

iOS Dev Nugget 224 Lazy Collections

.

Need to run a code review on your codebase? Hire me

Collection has a property called lazy which is very useful for performance reasons. From the docs, lazy:

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

Array implements Collection, so if you have a large array, you can do this:

var veryLargeArray = [1, 20, 30, 4, 55]
var c = veryLargeArray.lazy.map { $0 * $0 }
c[1] //Random access without processing the entire array

It's a tiny, localized change to your code that, under the right circumstances, can have incredible performance improvements. Since lazy is a property of the Collection protocol, you can use it with all collection types that implement it, including Dictionary and Set.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues