Which method for my inventory system would be more efficient?

I have an Inventory System and I’ve come across an issue between choosing which method would be best. Please note each item has A LOT of data looking something like this:

local ItemTemplate = {
Id = "",
Level = 1,
Experience = 100,
["Max Experience"] = 5000,
Tags = {},
Skills = {
TemplateSkill = {
SkillId = "Tackle",
Level = 1,
Experience = 100,
MaxExperience = 100
}
},
Enchants = {
-- more stuff here
TemplateEnchant = {
EnchantId = "Enchant",
Level =  1,
Experience = 100,
MaxExperience = 1000,
}
}
}

And as you can see the tables can stack quite quickly. (Sorry for the bad indenting)

Method 1 : Clear and Update
This method is generally very simple and requires barely any code. All I would need is 2 functions:

local function clearInventory()
-- delete everything in the inventory (UI)
end

local function loadInventory()
clearInventory()
-- load
end

Downsides:

  1. Sometimes if you have a lot of times users will notice a “flash” in the inventory updating it.

Upsides:

  1. Simple and not complex

Method 2 : Compare And Update

This step isn’t more complex but time-consuming and can be tricky. Every update, I’d have to loop through the current Inventory, compare items, look for missing items or items that shouldn’t exist and then take action, but this would require me to compare tables which would require a lot of iteration loops.

Downsides:

  1. Time consuming, can be tricky

Upsides:

  1. No flickering glitch

Between the 2, I’m not sure which would be better in terms of performance in code, but I’d like to know what I should be doing and is there an easy way to compare tables on the client.

From my point of view, the second method seems much more effective. Since it seems completely unnecessary to erase all the player’s inventory and then clone it again.

I don’t know how your inventory system is managed, but what you can do is, instead of a loop, update the inventory when the player buys something, or something like that (when you know the inventory has to change).
Or when you detect that the player added, or removed, some of his inventory, because with a loop, the player can have the same things in his inventory throughout the game, and the server would be checking throughout the game, which I don’t think that is entirely consistent.

I hope I have been helpful. And if I have not understood something, or if I have missed something, I apologize.

1 Like

The client is responsible for replicating the inventory and it is indeed true theoretically I could just log when something new is added or something is removed.

The Inventory does update when a change happens.

Thanks for the idea.

By the way, do you know how to easily compare tables?