Modern Javascript Engines are highly optimized when it comes to memory usage. Techniques such as internal key lookups and string de-duplication ensure that the length of key names has minimal impact on memory consumption.
To illustrate this, I conducted an experiment where I stored 1 million records in two arrays - one with long key names and the other with short key names. Surprisingly, both arrays consumed approximately 147 bytes per item. Even using a constant for the `vertical` key made little difference in memory usage.
If reducing memory overhead is a priority, TypedArrays could be a viable solution, although they may require more complex handling. By utilizing getters and setters, you might be able to bring down the memory footprint to around 33 bytes per record, comprised of 4 doubles and 1 byte.
However, before diving into optimization efforts, it's important to assess whether such optimizations are truly necessary. Premature optimization in Javascript can often lead to wasted time and resources.
I conducted these memory tests using NodeJs, which leverages Chrome's Javascript engine for execution.
For those interested in exploring how memory is impacted by different data structures, I provided a sample code snippet below that can be run in NodeJs:
const oused = process.memoryUsage().heapUsed;
const values = [];
for (let l = 0; l < 1000000; l += 1) {
values.push({
start: { x: 0, y: 0 },
end: { x: 0, y: 0 },
orientation: "vertical",
});
}
console.log(process.memoryUsage().heapUsed - oused);