When I was building a tiny anime streaming helper app (think watchlist + recommendations), I had a singleton object storing all my video metadata — basically a Videos object that held the list of episodes, their titles, and which season they belonged to.
At first glance, I just wrote:
object Episodes {
val all = listOf(
Episode("ep-1", "Naruto’s First Mission", 1),
Episode("ep-2", "Battle at the Bridge", 1),
Episode("ep-3", "Sasuke Awakens", 1)
)
}But then I thought — should I make this lazy?
So I rewrote it like this:
object Episodes {
val all by lazy {
listOf(
Episode("ep-1", "Naruto’s First Mission", 1),
Episode("ep-2", "Battle at the Bridge", 1),
Episode("ep-3", "Sasuke Awakens", 1)
)
}
}Turns out, this tiny by lazy change led me down a rabbit hole of understanding how Kotlin properties actually work.
🤔 What I Learned About object and val
objectin Kotlin = true singleton.
The JVM actually creates one instance and keeps it forever.A simple
val = listOf(...)inside an object is eagerly created once when the object is first referenced.Accessing it later just returns the same instance — it doesn’t rebuild the list every time.
by lazyonly defers the creation until the first access, but after that, it also caches the value and returns the same reference.
So in practice:
🎯 When to Use by lazy
✅ Use it when:
The computation is expensive (e.g., reading a file, parsing JSON).
The object might never be used (avoid startup cost).
❌ Skip it when:
The data is tiny and always used (like my episode list).
You already reference it in
init { }— it will get forced anyway.You want simpler code with no first-access synchronization overhead.
🧠 Mental Model
Think of val = listOf(...) like stocking all the anime DVDs on the shelf the moment your shop opens — they’re ready to grab.
by lazy is like keeping the DVDs in storage and only bringing them out the first time someone asks.
If people are guaranteed to ask for them every day, you just wasted a trip to the storeroom for no reason.
💡 Takeaway for My Anime App
In my case, the app always needs the list of episodes (to build the table of contents and check for missing IDs).
So eager val initialization made my code simpler and slightly faster — no lazy wrapper, no synchronization, no surprises.
✨ Final Thought
Kotlin gives us great tools like by lazy, but it’s not a magic performance booster — use it where it actually saves you work, not just out of habit.




