Open NullVoxPopuli opened 3 weeks ago
might be premature optimization - i'd guess a modern JIT would inline a constant function returning false and eliminate what's left of it.
that is an interesting point about JIT -- I made a little test to see what happens (perf.link)
I only have these two browsers atm:
false
is faster (which is what I would expect)false
, which is very surprisingNode and Bun are basically the same:
Modifying @NullVoxPopuli's test (perf.link) to include a loop results in the false
version being faster in FireFox (Dev, 126.0b1), Google Chrome (124.0.6367.202), and Safari (17.4.1) on an M1 MacBook Air running macOS 14.4.1
PS: Not sure if my version is benchmarking the right thing though, benchmarking is hard 😣
This is about more than the speed of calling the function: the signals system can have a more clear understanding of what's going on if it knows that it will always be false.
And to add on to that, if it's faster to always call, an engine will set its internal equals
field to a private pre-optimized global function that always returns false
.
Not that I even slightly find this likely, though. Not when it's only 3-4 instructions and 1 load
total compared to well over a dozen plus a multiply indirect call.
in options for both
Signal.State
andSignal.Computed
, anequals
function is allowed to be passed to change how "dirtiness" is calculated -- it could be an optimization to allowfalse
instead of requiring a function to then call every time -- we could skip part of the dirtying algo.thoughts?