Lua Performance Tips - About tables (2023)

About tables

Usually, you do not need to know anything about how Lua implement tables to use them. Actually, Lua goes to great lengths to make sure that implementation details do not surface to the user. However, these details show themselves through the performance of table operations. So, to optimize programs that use tables (that is, practically any Lua program), it is good to know a little about how Lua implements tables.

The implementation of tables in Lua involves some clever algorithms. Every table in Lua has two parts: the array part and the hash part. The array part stores entries with integer keys in the range 1 to n, for some particular n. (We will discuss how this n is computed in a moment.) All other entries (including integer keys outside that range) go to the hash part.

As the name implies, the hash part uses a hash algorithm to store and find its keys. It uses what is called an open address table, which means that all entries are stored in the hash array itself. A hash function gives the primary index of a key; if there is a collision (that is, if two keys are hashed to the same position), the keys are linked in a list, with each element occupying one array entry.

When Lua needs to insert a new key into a table and the hash array is full, Lua does a rehash. The first step in the rehash is to decide the sizes of the new array part and the new hash part. So, Lua traverses all entries, counting and classifying them, and then chooses as the size of the array part the largest power of 2 such that more than half the elements of the array part are filled. The hash size is then the smallest power of 2 that can accommodate all the remaining entries (that is, those that did not fit into the array part).

When Lua creates an empty table, both parts have size 0 and, therefore, there are no arrays allocated for them. Let us see what happens when we run the following code:

local a = {}
for i = 1, 3 do
a[i] = true

(Video) 44 - Tips on Table in Lua

It starts by creating an empty table a. In the first loop iteration, the assignment a[1]=true triggers a rehash; Lua then sets the size of the array part of the table to 1 and keeps the hash part empty. In the second loop iteration, the assignment a[2]=true triggers another rehash, so that now the array part of the table has size 2. Finally, the third iteration triggers yet another rehash, growing the size of the array part to 4.

----------------------- Page 6-----------------------20

A code like

a = {}
a.x = 1; a.y = 2; a.z = 3

does something similar, except that it grows the hash part of the table.

For large tables, this initial overhead is amortized over the entire creation: While a table with three elements needs three rehashings, a table with one million elements needs only twenty. But when you create thousands of small tables, the combined overhead can be significant.

Older versions of Lua created empty tables with some pre-allocated slots (four, if I remember correctly), to avoid this overhead when initializing small tables. However, this approach wastes memory. For instance, if you create millions of points (represented as tables with only two entries) and each one uses twice the memory it really needs, you may pay a high price. That is why currently Lua creates empty tables with no pre-allocated slots.

(Video) Lua in 100 Seconds

If you are programming in C, you can avoid those rehashings with the Lua API function lua_createtable. It receives two arguments after the omnipresent lua_State: the initial size of the array part and the initial size of the hash part of the new table.3 By giving appropriate sizes to the new table, it is easy to avoid those initial rehashes. Beware, however, that Lua can only shrink a table when rehashing it. So, if your initial sizes are larger than needed, Lua may never correct your waste of space.
如果你用C编程,可用Lua API函数lua_createtable避免那些再散列。它在随处可见的lua_State后面接收两个参数:新表的数组部分的初始尺寸和散列部分初始尺寸。3通过向新表提供合适的尺寸,很容易避免那些初始再散列。注意,无论如何,Lua只能在再散列时收缩表。所以,如果你的初始尺寸比需要的大,Lua也许永不修正你浪费的空间。

When programming in Lua, you may use constructors to avoid those initial rehashings. When you write {true, true, true}, Lua knows beforehand that the table will need three slots in its array part, so Lua creates the table with that size. Similarly, if you write {x = 1, y = 2, z = 3}, Lua will create a table with four slots in its hash part. As an example, the next loop runs in 2.0 seconds:
当你用Lua编程时,你可用构造器来避免那些初始再散列。当你编写{true, true, true}时,Lua预先知道表的数组部分需要三个存储槽,所以Lua创建具有那个尺寸的表。类似地,如果你编写{x = 1, y = 2, z = 3},Lua会创建散列部分具有四个存储槽的表。作为例子,下一段循环运行了2.0秒:

for i = 1, 1000000 do
local a = {}
a[1] = 1; a[2] = 2; a[3] = 3

If we create the tables with the right size, we reduce the run time to 0.7 seconds:

for i = 1, 1000000 do
local a = {true, true, true}
a[1] = 1; a[2] = 2; a[3] = 3

If you write something like {[1] = true, [2] = true, [3] = true}, however, Lua is not smart enough to detect that the given expressions (literal numbers, in this case) describe array indices, so it creates a table with four slots in its hash part, wasting memory and CPU time.
然而,如果你编写类似{[1] = true, [2] = true, [3] = true}的东西,Lua并非足够聪明得检测出所给的表达式(本例是字符数字)描述了数组索引,所以它创建一个散列部分具有四个存储槽的表,浪费了内存和CPU时间。(译者注——现在的版本似乎有这么智能了。?)

3 Although the rehash algorithm always sets the array size to a power of two, the array size can be any value. The hash size, however, must be a power of two, so the second argument is always rounded to the smaller power of two not smaller than the original value.

(Video) Lua Programming Tutorial | Episode 3 | Metatables and Immutable structures

----------------------- Page 7-----------------------21

The size of both parts of a table are recomputed only when the table rehashes, which happens only when the table is completely full and Lua needs to insert a new element. As a consequence, if you traverse a table erasing all its fields (that is, setting them all to nil), the table does not shrink. However, if you insert some new elements, then eventually the table will have to resize. Usually this is not a problem: if you keep erasing elements and inserting new ones (as is typical in many programs), the table size remains stable. However, you should not expect to recover memory by erasing the fields of a large table: It is better to free the table itself.

A dirty trick to force a rehash is to insert enough nil elements into the table. See the next example:

a = {}
lim = 10000000
for i = 1, lim do a[i] = i end -- create a huge table 创建一个巨大的表
print(collectgarbage("count")) --> 196626
for i = 1, lim do a[i] = nil end -- erase all its elements 擦除其所有元素
print(collectgarbage("count")) --> 196626
for i = lim + 1, 2*lim do a[i] = nil end -- create many nil elements 创建很多nil元素
print(collectgarbage("count")) --> 17

I do not recommend this trick except in exceptional circumstances: It is slow and there is no easy way to know how many elements are "enough".

You may wonder why Lua does not shrink tables when we insert nils. First, to avoid testing what we are inserting into a table; a check for nil assignments would slow down all assignments. Second, and more important, to allow nil assignments when traversing a table. Consider the next loop:

for k, v in pairs(t) do
if some_property(v) then
t[k] = nil -- erase that element 擦除那个元素

(Video) Faster than Rust and C++: the PERFECT hash table

If Lua rehashed the table after a nil assignment, it would havoc the traversal.

If you want to erase all elements from a table, a simple traversal is the correct way to do it:

for k in pairs(t) do
t[k] = nil

A "smart" alternative would be this loop:

while true do
local k = next(t)
if not k then break end
t[k] = nil

----------------------- Page 8-----------------------22

However, this loop is very slow for large tables. Function next, when called without a previous key, returns the "first" element of a table (in some random order). To do that, next traverses the table arrays from the beginning, looking for a non-nil element. As the loop sets the first elements to nil, next takes longer and longer to find the first non-nil element. As a result, the "smart" loop takes 20 seconds to erase a table with 100,000 elements; the traversal loop using pairs takes 0.04 seconds.

(Video) Lua 5.2 Tutorial 15: Weak tables and Finalizers


Is Lua coding hard? ›

Compared to other programming languages, Lua is not too difficult to learn. Many students are able to start embedding it into their applications after only a small amount of instruction. Because Lua is such a popular and relatively easy language, there are various ways for students to learn it.

Do people still use Lua? ›

Currently, Lua is popular in domains like gaming and web servers. Despite its age and lack of fanfare, developers still use Lua for a variety of things.

How fast is Lua compared to Python? ›

Performance: In terms of execution time, Lua is generally thought to be faster than Python. Python, on the other hand, has a larger standard library and a more comprehensive set of third-party libraries, which can make developing certain types of programs easier.

How fast is Lua compared to other languages? ›

The speed of using Lua modules can vary greatly, compared to similar markup-based templates, often ranging from 4x-8x times faster, or 180,000x faster when scanning text strings, which can exceed the 500-character parser-function markup limit as 64,000 characters or more.

Can you learn Lua in 1 day? ›

Roblox uses Lua and learning the basics of Lua can take anything from a few days to a few weeks, depending on the time you put into it. The more you practice, the faster you will learn. To get started and to become effective in Lua programming, there are some basics you should learn and know.

Can a 12 year old learn Lua? ›

While a little more complex than a visual programming option like those listed above, Lua is still a great programming language for kids and teens to learn – especially if they are interested in video games and robotics.

Is Roblox just Lua? ›

Code in Roblox is written in a language called Lua, and it's stored and run from scripts.

Why did Python beat Lua? ›

Python and NodeJS both beat Lua because they were scripting languages involved with web servers and web clients, and the massive influence of the web on the job market meant that everybody was going to want to learn Python (and Javascript) and then try to apply it blindly to everything.

Is Roblox built on Lua? ›

Luau is the scripting language creators use in Roblox Studio. It is a fast, small, safe, gradually typed embeddable scripting language derived from Lua 5.1.

Can you learn Lua in a week? ›

How Long Does It Take to Learn Lua? It takes two to three days to learn the basics of Lua, and three to four weeks to know enough to use it professionally. Lua's syntax is simple and fairly easy to learn.

What language is Lua closest to? ›

Lua is similar to Python in that it is an open-source programming language that is constructed on top of the C computer language. It is a lightweight programming language that is also capable of being extended.

Is Lua faster than Julia? ›

When I write out the same code for finding the smallest possible multiple of a number, Julia is much faster than Lua, both taking ~3 seconds & ~23 seconds respectively.

What is the fastest Lua compiler? ›

LuaJIT is often the fastest Lua runtime. LuaJIT has also been named the fastest implementation of a dynamic programming language. LuaJIT is sometimes hailed as competitive to the performance of C++.

What language is simpler than Lua? ›

Nelua is as simple and intuitive as Lua. Some additions like type notations, efficient data structures, and metaprogramming utilities are available, unlike Lua, but all of them are optional.

Which scripting language is faster than Lua? ›

daScript is: Extremely fast programming language that can rival compiled or JIT languages even in interpreter mode. It consistently outperforms dynamically interpreted scripting languages such as Lua.

What age is Lua for? ›

This is the perfect STEM gift for boys or girls ages 8-12 to learn Lua inside the Roblox coding for kids course.

Is Roblox Lua harder than Python? ›

“Roblox Studio and Lua is a great base language for students new to coding or looking for a bigger challenge than visual coding environments such as MIT Scratch,” said Johnson. “Lua is very similar to Python, it's really lightweight and really readable, but Lua is even more forgiving than Python.

What is the hardest programming language to learn? ›

Malbolge. This language is so hard that it has to be set aside in its own paragraph. Malbolge is by far the hardest programming language to learn, which can be seen from the fact that it took no less than two years to finish writing the first Malbolge code.

Is it OK if a 9 year old plays Roblox? ›

Content is generally suitable for ages 13 and up. May contain frequent moderate violence and/or light realistic blood.

Is Roblox inappropriate for school? ›

And though Roblox has some safety precautions in place, it remains a target for people with less-than-good intentions. Still, because of the learning potential Roblox offers, Common Sense Media rates it OK for users age 13+.

Can a 17 year old learn coding? ›

Coding is a skill that can be learned at any age. Many people who learn to code later in life go on to have successful tech careers.

Is GTA written in Lua? ›

So, in summary, while the engine that GTA V runs on it indeed written in C/C++, Lua can easily be included over the top of it as an embedded language for modders to use.

Why is Lua called Lua? ›

Lua 1.0 was designed in such a way that its object constructors, being then slightly different from the current light and flexible style, incorporated the data-description syntax of SOL (hence the name Lua: Sol meaning "Sun" in Portuguese, and Lua meaning "Moon").

Why doesn't Roblox use Python? ›

Does Roblox use Python? Roblox is not written in Python, and Roblox scripting is not compatible with the Python coding language, instead using Lua as the Studio programming language.

Why is Lua so small? ›

Lua was built for portability and is designed to have a small foot print. The standard library is small and has clean interfaces. Lua does not include a Regex library since: Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching.

Why is Lua so slow? ›

Roblox Lua runs over C++ which is why it is a bit slower than other programming languages. It's like taking a path to several places, you start at home, then school, then back home. Roblox Lua gets compiled in C++ then the result is given back to us in Lua.

Is JS faster than Lua? ›

Lua is also good in performance than any languages but it is not that faster say for a simple loop with one function call which executes on runs slower when compared to Javascript. But Lua can be made faster which is known as further improvement in Lua by using the LuaJIT compiler which speeds the rival codes.

What games are scripted in Lua? ›

  • Age of Conan.
  • Allods Online.
  • American Girl (video game series)
  • Angry Birds (video game)
  • Aquaria (video game)
  • Artifact (video game)

What game studios use Lua? ›

Leadwerks Game Engine uses Lua for user scripts. Lego Mindstorms NXT and NXT 2.0 can be scripted with Lua using third-party software. lighttpd web server uses Lua for hook scripts as well as a modern replacement for the Cache Meta Language. LÖVE, a 2D game framework for Lua (programming language).

What is the average salary for Lua? ›

Lua Programmer Salary
Annual SalaryMonthly Pay
Top Earners$141,500$11,791
75th Percentile$108,500$9,041
25th Percentile$57,500$4,791

How long will it take to master Lua? ›

It takes two to three days to learn Lua.

Lua is a fairly simple and easy programming language to learn. Typically you can learn the basics of Lua in two to three days, and know it well enough to use it at a professional level, in three to four weeks. There are different ways you can go about learning Lua.

Can a kid learn Lua? ›

As a language, Lua is great for kids and teens who want to pick up a language quickly, are eager to make the popular game their own, or are just interested in seeing how the worlds of gaming and coding collide.

Is Lua a toy language? ›

lua: A toy programming language written in Lua.

Is Lua a powerful language? ›

Lua is a powerful and efficient general-purpose programming language that was created in Brazil in 1993.

Why Roblox chose Lua? ›

Lua is a scripting language that is widely used in the gaming industry, and it is well-suited to being embedded in other applications. It is also relatively simple to integrate with other programming languages, which makes it a good choice for use in Roblox Studio.

Why is Lua so popular? ›

Lightweight and Fast

Although Lua is an interpreted language just like Python, it's very fast and lightweight. In fact, it is one of the fastest interpreted programming languages, consuming little memory.

What is the fastest high level language? ›

C++ is the fastest language according to a number of measures, including compilation time and execution speed. In this section, we will look at some ways in which C++ beats out other programming languages in terms of performance.

Which is better C# or Lua? ›

NET platform. C# (pronounced "See Sharp") is a simple, modern, object-oriented, and type-safe programming language. C# has its roots in the C family of languages and will be immediately familiar to C, C++, Java, and JavaScript programmers; Lua: Powerful, fast, lightweight, embeddable scripting language.

How old is Lua script? ›

Lua is a scripting language born in 1993 at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil.

What is harder Java or Lua? ›

Re: Lua vs Java? Java is similar to C++ and extremely similar to C#. You'll probably have a similar experience with Java as the other two, depending on what frameworks/libraries you use. Lua is far easier to use and simpler than Java.

How hard is Roblox Lua? ›

Roblox programming language, also known as Lua, is an easy way for kids to start their coding journey. It is an easy-to-use, powerful object-oriented language that focuses on scripting and can be used to make games. Due to its ease of use, it is not seen as a standalone programming language.

What does Lua mean in English? ›

British English: moon /muːn/ NOUN. The moon is the object in the sky that goes round the Earth once every four weeks and that you can often see at night as a circle or part of a circle. ... the light of a full moon.

Why is LuaJIT so fast? ›

The interpreter uses a custom bytecode format. The Lua 5.1 format needs a bit more bit fiddling to decode an instruction, but LuaJIT's format only uses fields that are multiples of 1 byte. This makes decoding instructions faster.

Which is harder Lua or Python? ›

Lua is easier than the Python language but Python is popular and demanding language than the Lua language for beginners. Python is a scripting language but it is heavy and slower than the Lua language but Lua is a light-weight, portable, and rapid execution language.

How long does it take to learn Lua? ›

It takes two to three days to learn the basics of Lua, and three to four weeks to know enough to use it professionally. Lua's syntax is simple and fairly easy to learn. Lua is “strongly typed”, which means that it is consistent and predictable.

Which is harder Lua or Java? ›

Re: Lua vs Java? Java is similar to C++ and extremely similar to C#. You'll probably have a similar experience with Java as the other two, depending on what frameworks/libraries you use. Lua is far easier to use and simpler than Java.

Is Lua really that fast? ›

Lua has well deserved reputation for being very small and fast. It was specifically designed to be an embeddable language rather like TCL. It is the most widely used embedded scripting language in game development.

Is C faster than Lua? ›

As documented here, Lua is implmented in C. It can only be as fast as C, but is more likely to be slower. It can't be faster than the language of its own implementation.

How much does a Lua coder get paid? ›

Lua Programmer Salary. $57,500 is the 25th percentile. Salaries below this are outliers. $108,500 is the 75th percentile.

Is Roblox Lua coding? ›

Luau is the scripting language creators use in Roblox Studio. It is a fast, small, safe, gradually typed embeddable scripting language derived from Lua 5.1. Use Luau in scripts to make your experience dynamic and interactive.


1. #18 Tables as Maps (LUA for GrandMA)
2. Ever wondered about LUA language usage? This info will guide you.
3. TABLES, OBJECTS and ITERATING through list of values in LUA (LOVE2D)
4. CppCon 2017: Matt Kulukundis “Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step”
5. Lua C API tutorial: using the stack and working with tables
(Tyler Neylon)
6. Full Lua Programming Crash Course - Beginner to Advanced
(Steve's teacher)


Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated: 09/26/2023

Views: 6122

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.