Link: https://try.haxe.org/#782Ea8D1
12:34:38:706 Test.hx:15:,testing...
12:34:52:920 Test.hx:48:,AVERAGE results:
12:34:52:921 Test.hx:50:,test "null check" took 0.07856666666666662 seconds
12:34:52:921 Test.hx:50:,test "null with empty" took 0.2395333333333334 seconds
12:34:52:921 Test.hx:50:,test "only empty" took 0.15176666666666686 seconds
12:34:52:922 Test.hx:52:,
12:34:52:922 Test.hx:53:,-------- THE END!
Null-check then call takes most time. It is the most common way to write the code.
Null-check without call (func is null
so no call) takes the least time. Again, it is the most common way to write the code.
Just calling the empty func takes is a middle result.
In situations when we are sure there will be many calls of the listeners, calling empty looks a better strategy. In situatons when we are sure the most listeners is null, null-check first looks a better strategy.