Interesting things highlighted.Factorio API wrote:Libraries and functions
2. New functions
pairs()
In standard Lua, the order of iteration when using pairs() is arbitrary. Because Factorio has to be deterministic, this was changed in Factorio's version of Lua. Factorio's iteration order when using next(), which pairs() uses for iteration, depends on the insertion order: Keys inserted first are iterated first. However, Factorio also guarantees that the first 1024 numbered keys are iterated from 1-1024, regardless of insertion order. This means that in usual usage, pairs() does not have any drawbacks compared to ipairs().
In Lua 5.2+, the iteration order is not deterministic, which leads to different results after each cycle. LuaJIT uses hashes to sort, but the order is always the same.
Example of pairs() output:
Code: Select all
+--------------+---------------+---------------+
! ! Factorio ! LuaJIT !
! local t = {} +---------------+---------------+
! t.aaaa = 1 ! [500] = 5 ! [2000] = 7 !
! t.aa = 2 ! [1000] = 3 ! [1000] = 3 !
! t[1000] = 3 ! ["aaaa"] = 1 ! ["aaaa"] = 1 !
! t.a = 4 ! ["aa"] = 2 ! ["aa"] = 2 !
! t[500] = 5 ! ["a"] = 4 ! [500] = 5 !
! t.aaaaa = 6 ! ["aaaaa"] = 6 ! ["a"] = 4 !
! t[2000] = 7 ! [2000] = 7 ! ["aaaaa"] = 6 !
+--------------+---------------+---------------+