Switch. A SwitchCase Simulation

This is two years old. Due to its age it’s most definitely not as efficient as it could be. This has no advantage over using if/elseif minus sugar.

If anything this is disadvantageous as it uses metatables very consistently and OOP is generally just slower than straight on code as Lua isn’t built with OOP in mind.

It’s also slower because if/elseif are built in to Lua which gives them great engine optimizations.


I have made a switch case module before but after the time since I’ve made it I’ve realized why Roblox doesn’t need it. It’s generally more clunky looking than if/elseif due to constant importation and how foreign it would look to other developers.

This just seemingly makes programming more bloated than using what the engine provides you for “better syntax” which is arguably deniable as it looks more like a Promise than a switch because of its reliance on OOP.

1 Like

The bytecode that if/else trees generate over common switch-case implementations is interesting. This creates a lot of protos and GETTABLEK calls which could be simplified with a few JUMPIFEQK calls.

Also this is very against how I’d approach this nowadays anyway :slight_smile:
(i dont use switches)

1 Like

Switches honestly are fine, actually even preferred - In languages that had them to begin with. Sadly there’s no good way of implementing a switch case system without making it more bloated and take up so much processing.

I also think this implementation didn’t implement fall throughs but I wouldn’t expect it to due to its simplicity.

Do you just use normal if statements now?