Skip to content

Instantly share code, notes, and snippets.

View reuschj's full-sized avatar

Justin Reusch reuschj

View GitHub Profile
@reuschj
reuschj / memoization_swift_properties.md
Last active July 26, 2020 19:48
Memoization in Swift Properties

Memoization of Swift properties

Introduction

Swift has a focus on being fast and efficient. To this goal, the memoization of computed properties would help to speed up some Swift programs relying on computed values (especially expensive ones) by only re-calculating the results when one of the properties they depend on has changed.

Anyone familiar with React and React hooks will know one of the most useful hooks is useMemo, which provides this exact functionality for rendering UIs. While memoization would be broadly applicable to any Swift program, it may be especially helpful for use in SwiftUI, where preventing unnecessary re-renders can help optimize app performance.

Motivation

Swift's use of computed properties is extremely convenient for dynamic properties that are derived from other properties and/or subject to change. That said, they can have a performance drawback if the programm