# Weak self reference vs strong member reference performance

**URL:** <https://forums.swift.org/t/weak-self-reference-vs-strong-member-reference-performance/57530>\
**Category:** Using Swift\
**Tags:** performance, weak\
**Created:** [May 23, 2022, 3:20pm UTC](https://forums.swift.org/t/weak-self-reference-vs-strong-member-reference-performance/57530 "2022-05-23T15:20:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjatie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjatie/32/24428_2.png) [@jjatie](https://forums.swift.org/u/jjatie)\
**Post date:** [May 23, 2022, 3:20pm UTC](https://forums.swift.org/t/weak-self-reference-vs-strong-member-reference-performance/57530/1 "2022-05-23T15:20:33Z")

</div>

What are the tradeoffs between capturing `self` weakly vs capturing a member on self strongly. Are there performance tradeoffs for strong vs weak captures?

Given the class below, are options 1 and 2 equivalent, or are there some tradeoff between the two?

```swift
final class Foo {
  let bar: Bar
  let networkService: NetworkService

  // Option 1
  func option1() {
    networkService.performSomeRequest { [weak self] data in
        self?.bar.handleData(data)
    }
  }

  // Option 2
  func option2() {
    networkService.performSomeRequest { [bar] data in
        bar.handleData(data)
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [May 23, 2022, 3:36pm UTC](https://forums.swift.org/t/weak-self-reference-vs-strong-member-reference-performance/57530/2 "2022-05-23T15:36:04Z")

</div>

There are some performance differences (explained below), but I don't think they're something to worry about unless it proves to be an issue.

I prefer strongly capturing the members I need, because there are more cases where I store a `self`-capturing closure into `self`, than cases where I would store `bar`-capturing closures into `bar`, so I find it to be a natural way to reduce accidental cycles.

However, that's only possible when `bar` is constant. If you want it to be immutable, you end up needing to capture `self`.

#### The perf differences

Weakly referenced objects need one extra memory allocation, and have more indirection when accessing their class/ref-count. If you're curious for more details, I'd recommend Mike Ash's article [Friday Q&A 2017-09-22: Swift 4 Weak References](https://www.mikeash.com/pyblog/friday-qa-2017-09-22-swift-4-weak-references.html)

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [May 23, 2022, 3:57pm UTC](https://forums.swift.org/t/weak-self-reference-vs-strong-member-reference-performance/57530/3 "2022-05-23T15:57:26Z")

</div>

Does handleData do anything useful if self was already deallocated? I think deciding whether you want to execute handleData in such cases has bigger performance impact than weak vs strong capture.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [May 23, 2022, 6:36pm UTC](https://forums.swift.org/t/weak-self-reference-vs-strong-member-reference-performance/57530/4 "2022-05-23T18:36:32Z")

</div>

Weak references definitely incur some performance overhead, but I'd strongly encourage not worrying about it unless a profiling tool like Instruments says you should. IMO the key thing with weak references is understanding your object graph and using them consciously with specific intent rather than just sprinkling them in and hoping for the best.

If you're in the _rare_ situation where a weak reference is both semantically necessary and a source of significant overhead, we can help brainstorm alternatives.
