Is this a bad way of structuring data?

local data = {
  {
    name:  'Cute White Bunny Glasses',
    id: 8519682333,
    itemType: 'Asset',
    assetType: 42
  },
  {
    name: 'White Mask',
    id: 5699850146,
    itemType: 'Asset',
    assetType: 42
  },
  {
    name: 'Retro Heart 3D Glasses',
    id: 6376988930,
    itemType: 'Asset',
    assetType: 42
  },
}

i have an array of objects all with the properties name,id,itemType and assetType. The problem is that i have 30000 of these objects, would it be better to structure each object like this {name,id,itemType,assetType} ? my guess is that it helps the game load faster, it also helps me to reduce the amount of lines in my module :stuck_out_tongue:

example:

{'Cute White Bunny Glasses', 8519682333, 'Asset', 42}

-- INSTEAD OF:

{
	name:  'Cute White Bunny Glasses',
	id: 8519682333,
	itemType: 'Asset',
	assetType: 42
}
2 Likes

As a single array instead of a dictionary?

yes, as a single array instead of a dictionary.

You mean something like this?

{'Cute White Bunny Glasses', 8519682333, 'Asset', 42}

-- INSTEAD OF:

{
	name:  'Cute White Bunny Glasses',
	id: 8519682333,
	itemType: 'Asset',
	assetType: 42
}

yes that is exactly what i meant

1 Like

Well it is true that standard arrays are much more performant that dictionaries. If you are willing to increase complexity for better performance, go ahead.

1 Like

Dictionaries (hash maps) are quicker to access/assign to but they come with the drawback of not being compatible with the unary length operator (#) and a majority of the table library functions.

local Hash = {}
local Array = {}

for Value = 1, 1000000 do
	table.insert(Array, Value)
end

for Value = 1, 1000000 do
	Hash[tostring(Value)] = true
end

local Clock1 = os.clock()
print(Array[500000])
print(os.clock() - Clock1) --0.000085

local Clock2 = os.clock()
print(Hash["500000"])
print(os.clock() - Clock2) --0.000028

The difference however is essentially negligible, so one should use whatever is most contextually appropriate.