3 ms·
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
by tasty_freeze 8y ago
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 }".
That was 2014, so likely things have changed.
- yosser 8y agoIronically, that's precisely how a lot of old Z80 and 6502 programs managed conditional execution. We used to call it a 'jump table'.