obj.method() are always “dynamic”. But this is not universally true for other languages. Let’s unpack the difference.
To understand this, we need to dive deeper into how computer code works under the hood. Generally speaking, the functions we write eventually become sequences of computer instructions that are stored somewhere in memory. Calling a function means the computer will “jump” to its code in memory and execute its operations.
When we say that a language has static dispatch, it means that when the computer compiles
obj.method(), it knows exactly which function this code would “jump” to. It won’t need to do any extra operations to “find” the function location in memory.
When we say that a language has dynamic dispatch, it means we can’t be 100% sure which function
obj variable, search for a property called
method on that object, keep searching for it on the prototype chain if needed — and finally, “jump” into the function (or error if it wasn’t found).
obj.method() call to match performance of a static call in a running program as long as their initial assumptions about your object remain true. The inverse may also happen, where a
fn() call that seemingly doesn’t involve “searching” for properties, may still require some extra steps. Small benchmarks are usually a lie, so you should generally let the engine do its job and write the code in the most natural way to you.
To sum up, “dynamic dispatch” means that when we write
Unless you’re specifically talking about the tradeoffs of
fn() calls or comparing different programming languages, saying “method call” is clearer.