Enum performance

Widely using enums, is there any performance hits when you make them complex?
Like, I have maps of enum classes and my enum can contain:

enum class Foo(@StringRes val nameId: Int, @DrawableRes val image: Int, bar: Int)
val buz = mutableMapOf<Foo, Int>()

Should they be as small as possible or it doesn’t matter? It should matter at least as a static object.

No, it shouldn’t affect the performance. Enums are like compile-time constants and at least in JVM they are compared by their identity (pretty much their memory address), not contents.

2 Likes

On JVM are available EnumMap and EnumSet.

2 Likes

There is a warning to strictly avoid using enums on Android because they take up twice the memory. Is it actual? How can I purge Enum if I use it only once? What if I use only 1 field of the enum and other fields are unused later?

That is a very, very outdated warning. Practically no one is recommending that anymore. It takes such a tiny little overhead that it’s barely noticeable, especially as the android runtime (ART) gets better and better.

1 Like

profile and see

1 Like

Do you mean - developers didn’t check the Enum performance?