1

I have a set of multiple tables(2d arrays), which each have different contents, but are the same structure.

Now i want to choose between one of those tables.

My first instinct was to use a 3d array, but I read that you shouldn't use these what other ways could i use ? Below is my current idea. I could of course just have many indidual arrays without array, but that seems even worse.

tables:[[[1, 2, 3],
         [1, 2, 3]],

        [[4, 5, 6],
         [4, 5, 6]]

        [[7, 8, 9],
         [7, 8, 9]]]
Selfie21
  • 57
  • 5
  • 1
    A 3d array may well be the most concise solution, were you encountering a specific problem with it? I don't think there's anything wrong with them in concept, though whether they're the most *appropriate* data structure depends on the situation – CertainPerformance Jan 19 '19 at 17:53
  • Thanks! I have no problem. Just wanted to know if anybody has a better solution or opinion on the solution! – Selfie21 Jan 19 '19 at 17:55
  • 3
    "*I read that you shouldn't use these*" - where did you read that, what was their argument? It seems there is nothing wrong with it. – Bergi Jan 19 '19 at 17:55
  • https://stackoverflow.com/questions/4943633/creating-and-parsing-a-3d-array-in-javascript says that i should really be sure to use 3d arrays, since you can achieve almost everything with 2d arrays. But I guess I sorta overreacted, Im pretty new to js – Selfie21 Jan 19 '19 at 18:00
  • It depends what you're modeling. I assume that question data isn't actually representative of your actual data. A nested array might not be the best solution, but until you explain what your data is we can't offer alternatives. – Andy Jan 19 '19 at 18:02

1 Answers1

1

If the arrays are small then just a 3d array is the way to go.

However if the arrays are big and they only contains numbers then it's better to flatten the structure out in a single typed array because that will allocate a lot less memory and also a lot less javascript objects.

For example an array 200x200x200 has 8000000 elements and at 8 bytes each (assuming floating point numbers) would take ~64Mb as when represented as a single Float64Array and is no problem at all for a current desktop computer.

Representing the same as an array of arrays of arrays of javascript values is going instead to be much worse because the housekeeping machinery is going to be more than the data itself and would put much more pressure on the garbage collector.

6502
  • 112,025
  • 15
  • 165
  • 265