Algorithm efficiency and alternatives to table.find

game is a table. Every time you do game.workspace, the hashmap lookup for the workspace key in that table has to happen (8e-7 seconds). That doesn’t sound much, but it’s considerably slower than using the globally predefined workspace variable.

1 Like

game is not a table, and is not subject to the rules of a standard Lua hashmap lookup. What is a table is the environment, which you access both when you do game and workspace.

Though, if we cared about both consistency and speed, local Workspace = game:GetService('Workspace') would probably be the best alternative.

2 Likes

That’s interesting. How’d you come by that info?

1 Like

I wanted to benchmark too. here are the results. (might be innacurate because i tested this in pure lua, which table.find does not exist in.)

0.27	 tablefind
0.01	 dictionary find

code

local testtable = {"hello","welcometothedigitalworld","wowowowiwow","digital","dhmis","amogus","listman","coolepicdude666","idk"}
function table.find(t,value)
    if t and type(t)=="table" and value then
        for _, v in ipairs (t) do
            if v == value then
                return true;
            end
        end
        return false;
    end
    return false;
end
local t1 = os.clock()
for i = 1,1000000 do
table.find(testtable,"dhmis")
end
print(os.clock() - t1," tablefind")
local dict = {}
--dictionarify cuz im lazy lmao
for _,v in pairs(testtable) do
dict[v] = 1
end
local t2 = os.clock()
for i = 1,1000000 do
dict["dhmis"] = 1
end
print(os.clock()-t2," dictionary find")
2 Likes

so youre saying if i have a table with 9999999 values. indexing is always instant according to the yellow line on the graph?