Lua templating/Converting Wikitext templates

Collected here are tips for converting Wikitext templates to Lua.

Switch statements
One of the most common cases of a slow template is due to a large  statement, which are often called multiple times on the same page. By making use of  and a simple table of data, we can make these templates significantly faster. By using a simple Lua table of data, we move to using a roughly O(1) hash table lookup, and by using, the data will only be loaded once in a page load no matter how many times Module:FooBar is invoked on a page. For a switch statement with 150 items, converting it to Lua in this way resulted in an approximately 50% improvement in parse time.

For example, if you have a template that uses a switch statement like so:

To convert it to Lua, create two module pages.

Module:FooBar:

Module:FooBar/data: