iOS Interview: Struct vs Class [Performance metrics- which one is faster]

Ravi Ranjan
CodeX
Published in
4 min readDec 25, 2022

--

Often we see a debate or generally an iOS interview question asking the difference between struct and Classes and when we should use struct and when should we use class.

Fundamentals:

In Swift, a class is a reference type, while a struct is a value type. This means that when you pass an instance of a class to a function or assign it to a constant or variable, you are passing a reference to the object, rather than the object itself. On the other hand, when you pass an instance of a struct to a function or assign it to a constant or variable, you are passing a copy of the object.

Here are some key differences between classes and structs in Swift:

1. Inheritance: Classes can inherit from other classes, while structs cannot. This means that a class can be a subclass of another class and can inherit properties and methods from its superclass. Structs, on the other hand, cannot have a superclass and cannot be subclassed.

2. Memory management: Classes use reference counting for memory management, while structs do not. This means that when you create an instance of a class, it is stored in the heap and a reference to it is stored in the stack. When there are no more references to the object, it is deallocated from the heap. Structs, on the other hand, are stored and passed by value, so they are always copied when they are assigned to a constant or variable or passed to a function.

3. Mutability: Classes can be mutable or immutable, while structs are always mutable. This means that you can change the properties of a class instance after it has been created, while the properties of a struct cannot be changed once it has been created.

When to use Class and when to use struct?

In swift we have two types of objects one is a value type and another one is a reference type. as discussed above classes are of reference type and structs are of value type other than that we have enums as well which are of the value type.

Application performance also depends on the usage of value type and reference type objects as when we create a class or struct and write any method then the code gets compiled and methods are tracked with a witness table so that we access those method compilers directly go to the witness table and execute the method. this process is called Dispatch [it is different for dispatch queue]. there are three types of dispatch:

1- Static Dispatch (it is also called Direct dispatch)

2- Dynamic Dispatch (it is also called Table Dispatch)

3- Message Dispatch

So whenever you call a function from a struct compiler directly goes to that method because a struct is a value type and the compiler knows there we only one value of that instance this dispatch is called Static Dispatch and this is the fastest one.

but when a function is called from a reference type of instance the compiler makes an array of function pointers (witness table ). And when a function is invoked, its address is searched in the witness table at runtime and then the invocation happens. this is because we can perform inheritance in reference type the compiler takes some time to execute the exact method by finding its references from the witness table.

So we can conclude that struct is faster and more efficient than classes however we can make our classes use static dispatch by tweaking some keywords like Final, and private.

Here are some additional considerations to keep in mind when choosing between a class and a struct:

Performance: Structs are generally faster and use less memory than classes because they are stored and passed by value rather than by reference. This can be important if you are working with large data structures or if you need to optimize the performance of your app.

Mutability: Structs are always mutable, while classes can be either mutable or immutable. If you need an immutable object, you should use a class and mark its properties as let.

Copy-on-write: Structs use copy-on-write optimization, which means that they are only copied when they are modified. This can be useful if you have a large data structure that you want to pass around, but you don’t want to pay the cost of copying it unless you need to modify it.

Reference cycles: Classes can create reference cycles, which can lead to memory leaks if they are not properly managed. Structs do not have this problem because they are always copied when they are passed around.

If you liked this, click the 💚 and give a clap on this post as much as you can below so other people will see this here on Medium. Also Please share this with your friends, and colleagues also on Linkedin and Twitter. If you have any queries or suggestions, feel free to comment or hit me on Twitter, or Linkedin.

--

--