In terms of performance and the data structure created, is:
function coords(xpos, ypos, zpos){
this.xpos = xpos;
this.ypos = ypos;
this.zpos = zpos;
return this;
}
var coordinates = coords(0, 0, 0); // notice I am not calling new
The same as:
function coords(xpos, ypos, zpos){
return {
xpos : xpos,
ypos : ypos,
zpos : zpos,
};
}
var coordinates = coords(0, 0, 0);
Is there are more performant way of generating the coordinates assuming I have a lot of them.
In the first approach for creating an object you have to use
var coordinates = new coords(0, 0, 0);and in javascript usingnewis expensive. In the second approach executing the function will return you a coordinate object.I prefer the second approach though it doesn’t make much difference in modern browsers.
here is a simple benchmark http://jsperf.com/objecr-vs-fn-perf-test
in Firefox 7 the first approach is ~75% slower than second and in chrome 17 it si ~30% slower