4 ms·
I understand that switch-cases may not be optimized well in JS, but what surprises me is that all the instructions in the cases are now grouped into functions a
by jraph 4y ago
I understand that switch-cases may not be optimized well in JS, but what surprises me is that all the instructions in the cases are now grouped into functions and this is faster than regular switch case.
When I write Javascript, I expect function calls to be slow. So I see three possibilities:
- (unoptimized) switch statement is very slow in JS, so slow that function calls will be faster
- function calls are not that slow in the end.
- both
It's a shame that the switch statement cannot relied on to be fast though. They are very readable, and indeed, feel like code that can be highly optimized. I guess that would risk making the JIT compilation too slow, or that not enough code found on the web rely on switch statements for this to matter too much.
Is this still true today? (I've seen a comment that says the 128 limit is still in V8's code)
How is this handled in SpiderMonkey, JavascriptCore or other JS engines?
- fenomas 4y agoTFA is basically about a limitation in V8's compiler, circa 2013. Based on the bug report it looks like that limitation was gone a few years later, and either way V8 switched to a whole different compiler back in 2017 or so.