We were discussing here about an optimization that...
# javascript
e
We were discussing here about an optimization that is performed on `object`s when they are stateless, or basically when they don't have properties, but only functions. In the case of an object with functions only, there is no lazy initialization. Would it make sense to document this optimization? @Artem Kobzar
a
As far as I know, we don't document our optimizations. I don't know the reason, but I assume that it could be because optimizations are implementation details, and some of them can be removed or changed. You mentioned that the optimization resulted in some degradations (performance or size?). Could you describe them?
e
Makes sense to generally not document them. This case is particular given how `object`s are used as namespacing mechanism. kotlinx-io is implementing unsafe operations, under an
object
as their namespace. What I was observing with `object`s is a performance penalty given there is a
VOID
check for every call, because of lazy initialization. Surprisingly, if the
object
doesn't have properties, but only functions, the initialization is eager and there isn't a
VOID
check on each call. This means the performance is the same as with top-level functions.
Copy code
// Eager init, no performance loss
object Example {
  fun example() { ... }
}

// Lazy init, performance loss
object Example {
  val prop = SomeClass()
  fun example() { ... }
}
So people might be interested in knowing about this.