I know that, in Lua, table keys are not preserved in order of creation. So far I thought it'd be due to the internal implementation which might continuosly rearrange the tree nodes in order to improve performance. However, I don't understand why the following snippet doesn't dump the same result even if it's executed in independent runs (e.g. running lua interpreter for each run):
-- Example script
local t = {a = 1, b = 2}
for k,v in pairs(t) do
print(k,v)
end
Sometimes the code above dumps either:
a 1
b 2
or:
b 2
a 1
How is it possible that, with so few lines (in which determinism seems obvious), the interpreter dumps different results? What makes keys order random here? Is the order random at table's creation or at table's iteration?
Thank you!