Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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]

  • No labels