In my Durandal-based SPA, I am utilizing the constructor below. Despite reaching out to the Durandal google group for help, I have not received a response yet. It is worth noting that the Durandal framework handles the instantiation of this viewmodel when the user navigates to the page.
function () {
var ctor = function () {
this.arr1 = [];
var arr2 = [];
this.getData = function () {
for (i = 0; i < 1000000; i++) {
this.arr1.push ({ empName: "mike", empAge: 30, empTitle: 'Senior Software Engineer' });
arr2.push ({ empName: "mike", empAge: 30, empTitle: 'Senior Software Engineer' });
}
alert("done");
}
};
return ctor;
});
When a button on the view triggers getData, only the objects in arr1 are garbage collected upon navigating away. The objects in arr2 remain intact.
I'm currently considering using "var" instead of "this" following some articles about creating private variables in this pattern. Is there any drawback to mixing "var" and "this"?
Shouldn't the JS GC handle the cleanup of both arrays? If so, then perhaps Durandal plays a role in this behavior.
The testing was conducted using Profiles in Chrome Dev Tools