Finding a value from a List
using closure .find(
) often causes a performance problem either:
(A) in case you do “finds” many items in the List
or
(B) if the closure .find()
is called frequently called (e.g. in the line item logics executed for many items)
Therefore the recommended approach is to convert the List
data to Map
first and use Map.get()
to find the value. You should use List.find()
only if you know it will not be called many times or on a list with many items.
Wrong:
def value = "someValue" def list = [...] // List of 10.000 map items def found = haystack.find(it.attribute == value)
Correct:
def value = "someValue" def map = [...] // List of 10.000 map items .group { it.attribute1 } // Map of 10.000 entries def found = haystack[value]