Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lua table, if the key starts from 1000, will there be a performance loss?

a={}
a[1000]=1

I don’t define other things. Will the storage space be occupied from 1 to 999?

I know that a[1]=nil or a[999]=nil, will it be calculated from 1 to 1000 in sequence during traversal?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

No, there is not going to be space allocated for other (1-999) elements (unless you create 1000 elements and then delete 1-999). Lua supports "sparse" arrays and will use the hash part of the table to store those key/value pairs.

If you are asking whether a[1000] is going to be slower if 1-999 elements are not present, then it’s possible, as in this case the hash part of the table is going to be used (instead of the array part), but you’ll have to benchmark to see if there is any observable difference that matters in your case.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading