Re:Why I prefer objects over switch statements
by tasty_freeze on Friday February 22, 2019 @12:31PM
(#19227169)
I wrote an old computer emulator in javascript. The inner loop is a large switch statement where each case handles one of 256 opcodes. Firefox handled it fine, but chrome performance was poor. It turns out that above a 128-way switch, the jit gives up (or it seemed to).
First I tried doing what the author suggested -- having 256 routines, and a dispatch table. Chrome performance got better, and Firefox performance got worse.
In the end, the fastest thing to do was to have "if (opcode < 128) { 128-way switch } else { 128-way switch }".
First I tried doing what the author suggested -- having 256 routines, and a dispatch table. Chrome performance got better, and Firefox performance got worse.
In the end, the fastest thing to do was to have "if (opcode < 128) { 128-way switch } else { 128-way switch }".
That was 2014, so likely things have changed.