This is an attempt to see if JIT-ed codepaths throw off a microbenchmark.
Running the microbenchmark produces
$ npm run benchmark
> @ benchmark .../fee7792dd695940483b3c0cdbfd8a78f
> node index.js
withDynamicDispath x 99,765,425 ops/sec ±2.36% (79 runs sampled)
withPolymorphic x 45,310,089 ops/sec ±1.61% (84 runs sampled)
The comparison is between invoking a function that accepts a polymorphic input (polymorphic since it can be a 2D or 3D point)
function polymorphicSum(point) {
return point.x + point.y + (point.z || 0);
}
and using dynamic dispatch to invoke two different specialized functions
function sum2D(point) {
return point.x + point.y;
}
function sum3D(point) {
return point.x + point.y + point.z;
}
These are invoked by "randomly" deciding at call time to use a 2D or 3D point so each of them will get invoked in the "same" way.