I have an application which currently calculates and stores data in the following way (obviously the case here has been very simplified, in reality there are many more properties in the inner object):
var processedData = [];
sourceData.forEach(function (d) {
processedData.push({
a: getA(d),
b: getB(d),
c: getC(d)
});
}, this);
function DoStuff(row) {
// Do Some Stuff
}
The number of objects created here can be high (thousands), performance is fine with the current approach but in the wider context I think it would really improve code readability and testability if I moved to a more defined object format:
var row = function (a, b, c) {
this.a = a;
this.b = b;
this.c = c;
this.DoStuff = function () {
// Do Some Stuff
}
};
var processedData = [];
sourceData.forEach(function (d) {
processedData.push(new row(
getA(d),
getB(d),
getC(d)
));
}, this);
There are two elements I'm worried about here, one is the performance/memory cost of constructing an instanced object with new. The second is the memory cost of including a function in the object which will have thousands of instances. I'm not sure how clever JavaScript is with that kind of thing.