I've been muddling through with Lua, and have been using output = output .. 'xyz'
throughout modules, then return output
at the end. But I read that repeated string concatenation isn't the best way of doing this. What is the recommended way for MediaWiki modules? Thanks :-)
Topic on Extension talk:Scribunto
For strings that get repeatedly concatenated to, a good method is to table.insert
the pieces into a table, then table.concat
when you want to return the string
In general, string concatenation will work just fine. It does use more resources than Dinoguy1000's method, as Lua will need to allocate memory for each intermediate string, and then garbage collect unused strings as necessary. However, it will not make an appreciable difference unless you are concatenating a lot of strings.
If you are concatenating less than, say, 100 strings to make your output, it's probably not worth the effort of switching from one style to another. It will probably need to be tens of thousands of string concatenations before you notice an actual performance difference between the two styles. Although, as with everything performance-related, you have to actually measure it to know for sure.
If you are willing to take on an additional dependency, you could also use something like Module:OutputBuffer. This module provides an easy-to-use wrapper for table.concat.