Skip to content
HebiKotei edited this page Feb 27, 2023 · 6 revisions

Tables

Use object access for constant keys, array access for non-constant

    local company = {
        boss = "Sam"
    }
    local boss = company["boss"]
    local boss = company.boss

When using table values multiple times, store it in a local variable

This increases readability, but also increases performance as you are not accessing the table constantly.

    local concatenation = myTable["key"] .. myTable["key"]
    local myTableValue = myTable["key"]
    local concatenation = myTableValue .. myTableValue

table.insert() sucks

It is not efficient/performant, you should only use this when you need to insert at a specific index.

Inserting at the end of a table

    table.insert(myTable, "value")
    myTable[#myTable + 1] = "value"

Inserting/Overwriting a given key

    table.insert(myTable, "key", "value")
    myTable["key"] = "value"