The filter
method runs passes each element in an array to the defined function and if the result of that function is true
then that element is included in the resultant array.
With enough elements, the second one is probably slightly faster because you only loop though all the elements once, test each element, and then send the command immediately upon getting the results of the test. In the first version, you loop though all the elements once to test whether to include them in a filtered array and then loop through the filtered array to send the commands, so you are iterating twice on any elements that pass through the filter.
I doubt there’s much of a performance difference between your two examples, unless you have thousands of lights, but I think that’s probably the (ever so slightly) more efficient version.
Just for completeness, sake, I find that the first command is much more readable. If you were to be using direct JS instead of Blockly, you can render the second command in the same format as the first:
items.getItem("gAllLights").members
.forEach( i => if (i.numericState == 0) { i.sendCommand("OFF") } );
But, even better, the helper library has a very nice shortcut for this:
items.getItem("gAllLights").members
.forEach( i => i.sendCommandIfDifferent("OFF") );
which gets you the efficient single iteration, only sending the command to items that need it, and maximum readability.
One last thing: If your items are Switch type, then i.numericState == 0
will not work. Only items with states that can be directly parsed to numbers will return a value with numericState
. So, I assume that all the lights you are testing are Dimmer type.