Lua templating/Optimisation

Optimisation is not generally necessary for needed, but may be done when they are misused.

Important note
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

General guidelines
If for whatever reason your modules are exceeding the limit, you should then consider optimising.

Whenever possible : Simple optimisations such as copying a global function (e.g. string.match) may greatly improve performance of a module.
 * Use local functions or variables
 * Avoid nested functions
 * Use arrays instead of tables (hashing)
 * Use local variables instead of indexing tables

Tip: Use os.clock before and after a code "chunk" and subtract the two results to evaluate the code section's  performance.

String vs table concatenation
When dealing with potentially  huge  strings, it is more efficient to add them to a table and use table.concat rather than simply using "join" or concatenating them.
 * -|Manual concatenation=


 * -|table.concat=

Use tables for SQL-IN alike compares
This means that instead of having a lot of if statements a better alternative is to put them all in a set-like table. For example, this is a typical  structure:

... and this is a more efficient, table-based one:

Note that you can use any truthy value, instead of just.

Templates
Using too many template transclusions in a single page is one common problem that affects the speed of lua. Each invocation will count towards the lua limit.

There are multiple techniques to overcome this:
 * Consolidate - move all code to a module, and restructure it to only be called fewer times


 * Rewrite the code to be more efficient.

Change template "databases" into data modules
Making templates databases causes a lot of issues such as becoming cumbersome to use, and very slower if they use parser functions. It is useful to add them to a module, as described in Lua templating/Converting Wikitext templates.

If the template is used several times on a page, consider using mw.loadData: