Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Finding a value from a List using the closure .find() often causes a performance problem in case of:

...

calling .find() closure on the List having many items

...

if you call the closure:

  • On a List with many items.

  • Too frequently (e.g. from an LPG line item on a big LPG).

The complexity of searching in a List is O(n). The complexity of searching in a Map is with high probability O(1). Therefore the recommended approach is to convert the List data to a Map first and use Map.get() to find the value. You List.find()should be used only if you know it will not be called many times or on a list with many items.

The Map can use any type object as a key. Therefore it is more recommended to use a List as key in case of multiple key fields instead of a concatenated Stringstring. Since concatenated String string depends on a chosen separator char and that can potentially lead to an error if the character comes in the data one day. Also It also makes the code worse to read and maintain for the other team members.

...

Warning

Correct use of .groupBy(), but wrong use of concatenated Stringstring:

Code Block
languagegroovy
def pricingRules = api.findLookupTableValues("PricingRule", ["key1", "key2", "key3", "attribute1"], null, null)
        .groupBy { record ->
            record.key1 + "|" + record.key2 + "|" + record.key3
        }
        
def pricingRule = pricingRules["CZ|Beef|A"]

...