Lua templating/Optimisation

Optimisation is generally only necessary when Lua modules are misused or inefficient.
== Important note ==
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." The fallacy of premature optimization http://ubiquity.acm.org/article.cfm?id=1513451
== General guidelines ==
If for whatever reason your modules are exceeding the limit, you should then consider optimising.
Whenever possiblehttp://www.lua.org/gems/sample.pdf:
* Use local functions or variables
* Avoid nested functions
* Use arrays instead of tables (hashing)
* Use local variables instead of indexing tables
Simple optimisations such as copying a global function (e.g. string.match) may greatly improve performance of a module.
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 http://www.lua.org/pil/11.6.html.
=== 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 tablehttp://stackoverflow.com/a/12865406. For example, this is a typical if structure:
... and this is a more efficient, table-based one:
Note that you can use any truthy value, instead of just true.
== 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 slower if they use parser functions. One idea is adding 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 to load its data, instead of require:
=== Avoid multiple invocations to the same module ===
Instead of this for example:
Do this:
== References ==
Category:Lua