Stefan Oltmann
05/02/2024, 10:09 PM{ person: Person -> person.age }
really the same thing as Person::age
in terms of performance and memory? Is the usage of the method reference also creating a new lambda that needs to be garbage collected after the method/function it was used in is done?
If so, this would surprise me. My feeling was that Person::age
should point to something that already exists instead of creating something new. And if used, it should be cached so that calling it 100x would not create 100 lambdas that need to be collected. 🤔
The only thing I found for was for Java and it said that method references are very slightly faster because they don’t need to capture the context.
( I did not test this myself so far with a profiler. )Youssef Shoaib [MOD]
05/02/2024, 10:16 PMStefan Oltmann
05/02/2024, 10:19 PMWhen you explicitly declare an object, a new instance is cre- ated on each invocation. With a lambda, the situation is different: if the lambda doesn’t access any variables from the function where it’s defined, the corresponding anonymous class instance is reused between callsRight. That would be the quote from Kotlin in Action v2 what I’m currently reading. So since method references capture no context they are lambdas that are never GCed?
Stefan Oltmann
05/02/2024, 10:25 PMYoussef Shoaib [MOD]
05/02/2024, 10:32 PMobject
declarations.