Performance of let vs var

Currently if I have an Array to show in UITableView , which I am not going to modify the best bet is to use a constant array let.

However , how do we understand the performance advantages of using a let vs var ?

1 Like

how do we understand the performance advantages of using a let vs var ?

I don’t worry about performance at this level. Rather, I approach this from a semantic perspective, using let if the data is immutable. That’ll make the code easier to maintain going forwards.

If my product doesn’t meet its performance goals I then profile the code to discover the hotspots. My experience is that my intuition about what will be slow is often incorrect, and thus it’s best to optimise based on profiling results.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

7 Likes

Are 'the lazy mapping collection' and 'explicitly define capacity for the arrays' two main sticks for improving performance on Swift?

As said above, you can't really be sure without measuring. Lazy collections postpone computations, but sometimes you deliberately won't want that, for instance, because loading a collection into the processor cache might be slower overall than doing all the mapping at once. And because performance gains become really only apparent for very repetitive tasks, an array expanding just a couple of times might not be worth optimising for.