- In Javascript I can represent a 2D point (1,2) either as an object
{x:1, y:2}or as an array[1,2]. Is the last one significantly better when it comes to performance? - If at some point of my program I decide to add a property to a point
v(such asv.next = u) andvis represented as an array, will I loose that [*] performance gain?
[*] assuming the answer to 1 is YES.
51 Answer
Taking my own advice (comment above), googling for 'array object point site:jsperf.com' leads to , which suggests that, at least in Chrome and Node.js (V8) today, objects may be be the better choice.
But if I wanted to put the pedal to the metal and was mostly working with arrays of points, I'd also benchmark code that forces all args to integers (x=x|0) and uses a one-dimensional array, performing the address arithmetic for x/y by hand, and writing an ES6 iterator for it. If done carefully, this can avoid creating an object for most points, even when manipulating them.