Sorry for the confusing title but basically I’m in a dilemma. So essentially I’m working on an object oriented wall cutter module, however there are some methods that require me to iterate over some of the tables’ contents, where these contents have indexes that are instances which means it doesn’t work with ipairs. So, my idea is to make a second table inside of my object that is just a table of instances that reference the indexes of the other table, thus creating a cross-reference (contents from one table referencing contents from another). Does the benefit of using cross-references outweigh the cons in this instance?
So for example,
function module.new()
local self = {}
self.Windows = {} :: { [Model]: { -- see here how the model is the index of the table
LeftParts: {};
RightParts: {};
TopParts: {};
BottomParts: {}
}
}
-- so my idea is to make a table inside of the object like so:
self._ref = {} :: {Model} -- a table of models who're being used as indexes in the self.Windows table above to be able to use ipairs instead of pairs
return setmetatable(self, module)
end
So then to iterate through it, instead of doing
for i,windowInfo in pairs(self.Windows) do
-- something for each window
I would do
for i,v in ipairs(self._ref) do
local windowInfo = self.Windows[v]
to use ipairs since, once again, ipairs is faster than pairs. There are pros and cons of each:
Method that allows for ipairs:
Pros:
- Faster
Cons:
- A bit complicated
- Memory usage is higher
Method that allows for pairs:
Pros:
- Easier to look at
- Reduced memory usage
Cons:
- Slower
Any thoughts/alternative approaches?