I have a system that looks like this:
interface Data {
x: number;
y: number;
n: string;
}
const array = Array<Data>(100);
I've read that in Chrome the V8 engine will allocate objects as C arrays if the array only contains the same type, but is it possible to actually check if my ´array´ object will act as a C array or dictionary, e.g. that the memory is contiguous allocated?
If this can't be done I know I could use a SoA model using TypedArrays like this:
interface Data {
x: Float64Array;
y: Float64Array;
n: ????;
}
const dataArray = {
x: new Float64Array(100),
y: new Float64Array(100),
n: ????????
} as Data
However I have no clue how to store Strings in an array like this
V8 developer here. var dataArray = new Array(100)
will give you a contiguous array of 100 pointers, so in that sense will be "C like", not a dictionary. dataArray[0] = new Data(x, y, n)
will allocate a new Data
object outside of the array and store a pointer to it in the array's first slot. Within such a Data
object, on 64-bit platforms, V8 can store numbers directly in the object (x
and y
in your example), whereas strings and other objects will always be stored as pointers to another object. Within an array that only contains numbers, V8 can also store them directly in the array, so you don't need to use a Float64Array
for that.
All that said, any performance differences are probably too small to matter, so I would recommend that you write the code that makes the most sense to you (is most readable, fits your algorithm/data model best, etc).