top of page

Use lazy var to Improve Performance

Use lazy var to Improve Performance

If you have a property that is expensive to compute and only needed when first accessed, declare it as lazy.

Example:

class UserProfile {

    lazy var fullName: String = {

        print("Computing full name...")

        return "\(firstName) \(lastName)"

    }()

    

    let firstName = "John"

    let lastName = "Doe"

}


let user = UserProfile()

print(user.fullName) // "Computing full name..." printed only when accessed

print(user.fullName) // Uses cached value, no extra computation



Why Use lazy?

The property is only initialized when first accessed, saving memory and processing time.

Ideal for expensive operations like database queries, API calls, or heavy calculations.

 
 
 

Recent Posts

See All
{{ $('Get Ready Post').item.json.Title }}

AI agent workflows are shifting enterprise spending. Gartner predicts AI inference costs per agentic workflow will increase more than 5x through 2028 as systems shift from basic LLM prompts to continu

 
 
 
{{ $('Get Ready Post').item.json.Title }}

Cloud-based AI has two persistent problems for mobile developers: latency, because every inference call is a round trip to a server, and privacy, because user data has to leave the device to be proces

 
 
 

Comments


© 2025 by Shahzad. Powered and secured by QMShahzad

bottom of page