3 ms·
I like function tables. I think they are cleaner than switch statements But, (maybe this is an invalid test), checking today my results suggest a switch is fas
by greggman3 4y ago
I like function tables. I think they are cleaner than switch statements
But, (maybe this is an invalid test), checking today my results suggest a switch is faster than a function table in 2022? At least at a base level. Maybe the more complicated the code for each case gets the less likely it is to get optimized.... which is true in general? large code blocks are less likely to get optimized and a switch is considered one large code block.
https://jsbenchit.org/?src=5c1e5bcf62448b6d1750b12893b0f0a1 https://jsbenchit.org/?src=5c1e5bcf62448b6d1750b12893b0f0a1
- whent 4y agoAre function tables really all that cleaner? Honest question. If I can offer a counterpoint a function table requires us to code some function dispatch routine that looks at the intention (keywords in function table), then looks up the function, then finds a way to bind the arguments to the function (language specific) and finally call the function? Isn't this more complexity in code? Isn't switch-case simple because it is available as a construct in the language?
- deleted 4y ago[deleted]
- masklinn 4y agoThe article is a decade old. In the meantime the entire v8 execution pipeline has been rewritten: a new optimising compiler was swapped in, a baseline interpreter was added, and the baseline compiler was removed (in more or less that order iirc). Wouldn’t at all be surprising if that lack of optimisation had been long fixed.
- vanderZwan 4y agoNitpicking on an otherwise insightful comment: the article is recent, but the period in which the optimization was made was decade ago.
- no_time 4y agoStaggering difference between Spidermonkey and JSCore. I hope the situation isn't as dire on linux :/