Context
I am building a Redux application, and would like click
and mousemove
event handlers to change depending on application state.
To accomplish this, I am planning to attach a single click
and single mousemove
handler to the document
. These will belong to a scope that subscribes to the Redux store, and knows the relevant state.
I see two strategies for implementing the handlers:
- Use a
switch
statement against the relevant state variable. Thisswitch
would have to be called for each fired event. - Reference internal (to the scope)
scope._clickHandler
andscope._moveHandler
properties that swap out their referents whenever the state is updated.
Question 1:
If I choose Strategy 2, I will want my mousemove
handler to do nothing most of the time. But mousemove
gets fired a lot! What is the most performant no-op function that could be bound to the scope._moveHandler
property?
Question 2:
In the best case (w/r/t Question 1), would Strategy 2 be more performant than Strategy 1, assuming the switch
statement used in Strategy 1 has to test between 10 and 100 cases?
Clarification
On this approach, one "delegating" function will always be called when an event fires. That function will do different thing depending on a state property, either by matching that state in a switch
or simply by having different "delegee" functions bound as the state.
Question 1 asks about the optimal do-nothing "delegee" function, while Question 2 asks about the performance penalty incurred by having a large switch
block in a mousemove
callback.
If we as a community agree with @nnnnnn's intuition that function(){}
is as good as it gets for a no-op, my question becomes:
Which is worse for performance: the overhead for the no-op function call, or the equality comparisons in a switch
block?