1. 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?
  2. If at some point of my program I decide to add a property to a point v (such as v.next = u) and v is represented as an array, will I loose that [*] performance gain?

[*] assuming the answer to 1 is YES.

5

1 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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy