When I am appending data to a Lua table an idiom I use a lot is:
T[#T+1] = 1
I noticed a lot of people use the Lua table insert function which is like:
I tend to prefer the former expression since I am used to it. I was curious if there was any performance benefit one way or the other.
I wrote some test code and found that table.insert took twice as much time in the editor. This is because of the fact it creates annotations which has a little bit of overhead. Running the channel in production without the overhead of annotations and the table.insert method seems to be marginally slower than T[#T+1] = 1.
Screen shot of the logs.
And the annotations.
I dropped the channel into a github repo if anyone is vaguely interested 🙂 :
Did another test comparing ipairs vs. a for loop.
ipairs is slower in the editor due to annotations but faster than the simple for loop at runtime.
You must be logged in to reply to this topic.
Please don't hesitate to take anonymous feedback survey or leave us a comment.