0

Does anyone know whether there are any performance trade-offs/advantages to assigning an object's fields at creation rather than later e.g.

var exObj = new exampleObject(name, date);

OR

var exObj = new exampleObject();

exObj.name = "blah"; exObj.date = "blah";

(assuming you've created your class accordingly)

Also, as a side thought, given that JS arrays are stored as objects, am I correct in assuming that there are no performance differences between using one over the other ? (For some reason using an array with a numeric index "feels" faster.)

Cheers

N

1 Answer 1

1

Test it yourself - http://jsperf.com/assign-object-fields-at-creation-or-later

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for this - I actually had planned to test it myself if there were no responses but I hadn't used/been aware of jsperf.com before so this simplifies things considerably. For those who are interested, the latter method is fractionally quicker (at least on FF 3.6.13).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.