Skip to content

Instantly share code, notes, and snippets.

@T1mL3arn
Created February 17, 2024 02:49
Show Gist options
  • Save T1mL3arn/f6e37b735858617d439bce023167a50d to your computer and use it in GitHub Desktop.
Save T1mL3arn/f6e37b735858617d439bce023167a50d to your computer and use it in GitHub Desktop.
Testing: null check then function call time measurement

Link: https://try.haxe.org/#782Ea8D1

Results

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!

Interpreting results

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment