Increasing speed of findFirst in rules DSL

I was having issues with the speed of some of my rules (delay between switch press and light turning on was too long) and after some debugging, located the problem: using members.findFirst is causing delays. Specifically, lines like:

var root_name =  "" + triggeringItem.name.split('_').get(0) + "_" + triggeringItem.name.split('_').get(1) + "_" + triggeringItem.name.split('_').get(2) + "_" 
var state_item      = LightVirtual.members.findFirst[ i | i.name == root_name + "State"]
var brightness_item = LightVirtual.members.findFirst[ i | i.name == root_name + "Brightness"]
var brightness_mem_item = LightVirtual.members.findFirst[ i | i.name == root_name + "BrightnessMemory"]
var colortemp_mem_item  = LightVirtual.members.findFirst[ i | i.name == root_name + "ColorTempMemory"]

In this case, the code takes about 400ms to run with a group size of around 200 members–this is too long.

Short of decreasing the group size, is there a faster way to do this? Is there a method I can use provided that I have the exact name of the item like items.get(name) as is possible in the MainUI widgets with items[<item name>]?

For some years now, yes

1 Like

Thanks, this is great! I wish more people/examples would’ve used this–everywhere I looked, all I found was findFirst :smiley:

About a 20x increase in rule execution speed: what took 400ms before now finishes in 20ms.