When do Lua strings get instantiated?

Viewed 37

Suppose I have the following loop:

for name in poll() do
    if name == "quit" then
        return 0
    end
end

Will the "quit" string be re-created and cleaned up with every iteration, or will Lua re-use the same instance?

1 Answers

It is just created one time and then it is "internalized", that it is saved in a hash table and then reused.

From The Implementation of Lua 5.0

Lua internalizes strings using a hash table: it keeps a single copy of each string with no duplications.

Related