Hello, I have made this table compare-or to assert weather two tables are identical or similar.
The issue is that this will be ran a lot and I know this will hinder performance.
Just to note, I have already attempted optimization and it dose work correctly.
Anyway, How can I further optimize the following code?
function CompareTables(tabA, tabB)
local Same = true
local Similar = true
local keysA = {}
local keysB = {}
local function getResult()
if Same then return "Equal" end
if Similar then return "Similar" end
return "Different"
end
for k, v in pairs(tabA) do
keysA[k] = v
end
for k, v in pairs(tabB) do
keysB[k] = v
end
for k, v in pairs(keysA) do
if keysB[k] == nil then
Similar = false
local result = getResult()
return result
elseif keysB[k] ~= v then
Same = false
end
end
for k, v in pairs(keysB) do
if keysA[k] == nil then
Similar = false
local result = getResult()
return result
elseif keysA[k] ~= v then
Same = false
end
end
local result = getResult()
return result
end
The Above function has been replaced with a module
below that I have developed based on feedback
local httpService = game:GetService("HttpService")
local TableUtility = {}
TableUtility.ComparisonStates = {
Equal = 0, --Same keys, same values
Similar = 1, --same keys, different values
Different = 2-- lacking a key
}
local function isType(value, ...)
local args = table.pack(...)
local valueType = type(value)
if valueType == args[1] then return true end
for k, v in pairs(args) do
if valueType ~= v then return false end
end
return true
end
--This will not support mixxed tables
function TableUtility:ExtractKeys(table1)
local keys = {}
for k in pairs(table1) do
table.insert(keys, k)
end
return keys
end
function TableUtility.TablesHaveSameKeys(table1, table2)
local keys1 = TableUtility:ExtractKeys(table1)
local keys2 = TableUtility:ExtractKeys(table2)
return (httpService:JSONEncode(keys1) == httpService:JSONEncode(keys2))
end
function TableUtility.AreValuesEqual(value1, value2)
if value1 == value2 then return true end
local areValuesTables = (
isType(value1, "table")
and isType(value2, "table")
)
if not areValuesTables then return false end
local areTablesEqual = TableUtility.DoTablesMatch(value1, value2)
if areTablesEqual then
return true
else
return false
end
end
function TableUtility.DoTablesMatch(table1, table2)
local baseEquivalence = (table1 == table2)
if baseEquivalence then return true end
for k, v in pairs(table1) do
local matches = (
TableUtility.AreValuesEqual(table2[k], v)
)
if not matches then
print(matches)
return false
end
end
for k, v in pairs(table2) do
local matches = (
TableUtility.AreValuesEqual(table1[k], v)
)
if not matches then
print(matches)
return false
end
end
return true
end
function TableUtility:CompareTables(table1, table2)
local areTablesEqual = self.DoTablesMatch(table1, table2)
if areTablesEqual then
return self.ComparisonStates.Equal
end
local areSimilar = self.TablesHaveSameKeys(table1, table2)
if areSimilar then
return self.ComparisonStates.Similar
else
return self.ComparisonStates.Different
end
end
function TableUtility:DoQuickTableComparison(table1, table2, comparisonState)
if comparisonState == self.ComparisonStates.Equal then
return (self.DoTablesMatch(table1, table2))
elseif comparisonState == self.ComparisonStates.Similar then
return (self.TablesHaveSameKeys(table1, table2))
elseif comparisonState == self.ComparisonStates.Different then
return (not self.TablesHaveSameKeys(table1, table2))
end
end
return TableUtility