Tables, which method of searching is faster

Just realized that i posted this here instead of Lua scripting. Oops.

Say I have two different ways of storing data.

I only need to check if the table has the data.

Say our value is the word “Slash”

Method 1 (non integer indexed table)
tbl = {}
tbl[“Slash”] = true

if tbl[“Slash”] then return true end

OR Method 2 (integer indexed)

tbl = {}

for i,v in ipairs(tbl) do
if v == “Slash” then return true end

I usually use the first method, never have known which is faster though.

I would imagine method 1, for this particular case, since you’re not actively iterating through the entire table.

I may be wrong, though.

Use your common sense?

First, due to hashing.

Not that it really matters unless the table is massively massively huge, or just massive huge but being called very often.

First due to the fact you specified which part of the table to use. The second would go through the entire table rather than just going to the referenced index of the table.

If you don’t mind, I have a similar issue at Would be so grateful if someone could answer it :3

I’d agree with #1 in this case. You don’t really know what algorithm the language uses to grab the value at that index, but chances are that such a common operation is probably very well optimized by the language already.