What are the performance advantages and disadvantages for using mutation in a struct in Swift

I have used some functional program to avoid mutating the structure and there is no clear explanation of which approach is best in terms of performance.

Can anyone please help and suggest the best solution in terms of performance and memory management in this case?

For example:

Mutation variant

struct User {

    var name:String

    init(name:String) {
         self.name = name
    }

    mutating func change(name:String){
        self.name = name
    }
 }

      

Optional Variant

 struct User {

    var name:String

    init(name:String) {
         self.name = name
    }

    func change(name:String) -> User {
        return User(name: name)
    }
 }

      

+3


source to share





All Articles