Fake Multidimensional Array Method?

Hey there. I just wanted to ask which one of these ways is better for my use case.

I found a solution online to use a special trick to have a multidimensional array by useing “fake” arrays

I am trying to create a fluid simulation in Roblox and I would like to know what your thoughts are on which one would be better to use for my case. I don’t plan on having the fluid exit the simulation area It will change a lot of how I will program the stuff so if you could choose that would be great

-- Trick way
local function IndexX(Position: Vector3, Size: Vector3)
	return ((Position.X) + (Position.Y) * Size.X + (Position.Z) * Size.X * Size.Y)
end

-- Normal way
local function Create3DArray(Size: Vector3, DefaultValue: any)
	local Array_3D = {}
	for x = 1, Size.X do
		Array_3D[x] = {}
		for y = 1, Size.Y do
			Array_3D[x][y] = {}
			for z = 1, Size.Z do
				Array_3D[x][y][z] = DefaultValue
			end
		end
	end	
	return Array_3D
end

-- Normal
local 3dArray = Create3DArray(Vector3.new(2,2,2),0)
3dArray[2][1][2] = 10
--[[
{
{{1, 1},
{1, 1},
{1, 1}},

{{1, 10},
{1, 1},
{1, 1}}
}

]]

-- Trick
local Array = {}
Array[IndexX(Vector3.new(2,1,2),Vector3.new(2,2,2] = 10
-- {1,1,1,1,1,1,1,10,1,1,1, 1}

Does it make a difference? Is there a better way to do this? I will be indexing this A LOT during the frame so it would be nice to know which on is better for performance.

the trick method has more performance because it makes an array using math

the normal method has less performance because it makes a matrix using for loops

Creating the data structure is probably a one time thing, so it doesn’t matter so much how long it takes. It’s more important how long it takes to do lookups and make changes to the table. The only way to know for sure which approach is faster in this regard is to test it.

I was mainly answering the performance question, didn’t say which was best overall

Actually I feel that using the first method is more straightforward really for me.

The only downside to the first method is that I am only able to change 1 axis. But for now that will do. Thanks for your help. I think I will be using the first way.

Also I did test it. Was a small improvement on the normal way.