Problem
- I have a few arrays, each of 2000 numbers which are any one of 0, 1, or 2.
e.g:
array1 = [0,0,0,0,1,2,2,2,2,0,2,2,0,1,..etc]
array2 = [1,1,1,1,1,1,2,2,2,2,0,2,2,1,..etc]
I want to test for relatedness, so ideally each element in these arrays should have two properties:
- the data: 0, 1,or 2
- a pointer to another array (should the data match for the same index, or null)
I'd prefer not a direct copy, but it's a not a must.
My attempt 1
I thought of converting each element in the array into an Object
{
data: 0, // or 1 or 2
pointer: other_array[some_index].data
}
(I'm hoping here that blah.data returns a pointer and not the data)
But this feels like it would slow down the retrieval of data, since they are now likely scattered about in non-contiguous memory.
My attempt 2
Instead of having an Object array I thought I'd convert each array into two associative arrays:
my_array = {
data_array: [0,1,2,2,2,2,1,1,1,1,1,2,2,2],
pter_array: [o_a.data_array[0], o_a.data_array[1], o_a.data_array[2], ... ]
};
(where o_a is some other_array)
This way it seems like the data_array at least will be more contiguous, and retrieval will be hopefully faster, but my pointer array might not be pointers.
Questions:
- Which implementation is better? Is the second attempt any faster than the first?
- JS seems to only have 64-bit numeric type. Do I have to use 64-bit floats to store my numbers when a byte would more than do?
- Given that my data is no more than 3 different values, would it be any more efficient to implement an
enum(via bools?), or would that waste just as much space as the JS numeric type?
- Given that my data is no more than 3 different values, would it be any more efficient to implement an