3 ms·
> The code below that paragraph actually does check on every access, though. I could be wrong, but I think a sufficiently clever JS engine (like v8) would remo
by fenomas 8y ago
> The code below that paragraph actually does check on every access, though.
I could be wrong, but I think a sufficiently clever JS engine (like v8) would remove that check at runtime, by realizing that half of the ternary operator is dead code. (If it's hot code and it gets optimized, anyway).
- braythwayt 8y agoAh, the mythical “sufficiently smart compiler” :-)
- fenomas 8y agoTo clarify I wasn't being hypothetical, I think v8 will optimize out that check today. I phrased it vaguely because I'm not sure whether other engines would as well.
- braythwayt 8y agoIt’s certainly doable. The `defaultValue` parameter is passed by value, so it will never be rebound. The result of using `typeof` on a value never changes in JS, even if the value is modified. So... it should be possible to optimize the check away after the first time.