Exploring the relationship between Javascript constructors and memory management

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

Answer №1

The cleanup of your data by the garbage collector occurs whether it resides in a local (private) variable or an instance variable, once the containing object is available for GC. It does not have an impact from that particular standpoint.

Private variables offer value and are a valid coding approach. They necessitate that all methods accessing them be defined in the constructor, similar to how your .getData() method is specified. The use of the prototype is not feasible for methods requiring access to private variables defined within the constructor because prototype methods exist in a separate scope.

To delve deeper into the private member variable design pattern, you can refer to this source:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Should information be retrieved from the server or the client side?

Allow me to clarify the situation at hand. I am currently developing a website where users can store movie information, sourced from a third-party API known as The Movie Database. At this juncture, my main concern pertains to optimizing performance throu ...

jQuery ajax issue: Unable to locate file using relative path

I have recently transferred some script from an HTML document to an external file. I updated the jQuery path to point to the new location of the JavaScript file, but now it is unable to locate the necessary PHP file. Can anyone shed light on why this might ...

Determine the array with the highest value by comparing multidimensional arrays in JavaScript

I have a multidimensional array with names and corresponding integer values. I want to compare the integer values within each nested array to find and return the one with the highest value. var nums = [ ['alice', 'exam', 60], [ ...

Utilizing a dynamic form action connected to an Express route

I've been grappling with creating an HTML form in my nodejs application that directs to the appropriate express route upon submission. After researching online, I stumbled upon a potential solution as outlined below: <script> $('#controlPa ...

Is there a way to display "no results" on the page when the search field is empty, yet still render new results asynchronously while searching?

I am implementing a feature where buttons are dynamically rendered on the page based on user input. Each time the user types a letter, a new set of buttons is displayed on the page using an obj.content that contains the string being typed. Although this fu ...

Common issues encountered when using the app.get() function in Node.js

I've been attempting to develop a website that utilizes mongojs. I've implemented the code snippet below, but when I launch the site, it never reaches the app.get() section, resulting in a 500 error on the site. How can I ensure it responds to th ...

Implementing Ext JS Checkbox Inside a Row Editor

Does anyone have a solution for aligning the checkbox in a RowEditor to the center position? I am using the "triton" theme with Ext JS 6.0.0. The issue is that the checkbox is currently placed at the top of the row, while other fields like textfield or co ...

Dividing a model using three.js

Imagine you have a basic raycaster set up. When you hover over it, the model illuminates. In this scenario, the model is broken down into separate parts, each its own "model" within the larger whole. For instance, think of a car model - when you hover ove ...

Looking for a way to convert 24-hour format to minutes in Angular? I'm in need of some TypeScript code to

I am looking for Typescript code that can convert 24-hour time format into minutes. For example, when converting 1.00 it should output as 60 minutes. When converting 1.30 it should equal to 90 minutes. If anyone has the code for this conversion, p ...

Creating a link with a POST method and without using a new line

I am attempting to generate a square matrix and send data using the post method. I have discovered a solution involving JavaScript and form submission, but each time a new form is created, it results in a new line being added. Alternatively, if I move th ...

Having trouble getting the items to show up on the canvas

I have been struggling to implement JavaScript on a canvas in order to display mice in the holes using the mouse coordinates. Despite trying many different methods and spending close to a month on this project, I still can't seem to get it to work acr ...

Transform JSON code from JQuery to PHP

Currently, I am in the process of translating a code snippet from JQuery to PHP for performing a json POST request to a remote server. Here is my original Jquery code: $( document ).ready(function() { $('#button').click( function() ...

What steps should I take to host a Node.js application on a subdomain using an apache2 VPS?

Currently, I have a Node.js application that I would like to host on a subdomain using my VPS. The VPS is running apache2 and the Node.js app utilizes Express. Despite trying both Phusion and following this tutorial, I have been unsuccessful in getting i ...

Uncovering the Model within a controller in Ember JS

I am attempting to extract the original model object from a controller for the purpose of persistence (without utilizing ember-data). The straightforward approach would be: controller.get('content'); However, this method is not effective. The i ...

Is it feasible to merge Apollo queries within the context of Nuxt?

Incorporating nuxt and apollo together using the https://github.com/nuxt-community/apollo-module module has been a successful venture. A GraphQL query was crafted and tested in GraphiQL to obtain information about a specific page along with general SEO de ...

Help with enabling the recognition of backspace key in JavaScript?

My current JavaScript is almost perfect but missing one key element. The form has two fields that are disabled when the user fills it out. <label>Can you drive? </label> <input type="booleam" id="drive" disabled><br> <label>W ...

What is the best method for removing extra spaces from an input field with type "text"?

I have an input field of type "text" and a button that displays the user's input. However, if there are extra spaces in the input, they will also be displayed. How can I remove these extra spaces from the input value? var nameInput = $('#name ...

What is the best way to populate nested elements with jQuery?

I am trying to showcase the latest NEWS headlines on my website by populating them using Jquery. Despite writing the necessary code, I am facing an issue where nothing appears on the webpage. Below is the HTML code snippet: <div class="col-md-4"> ...

Enter the Kannada language into the HTML text box or input field

My html form consists of about 15 - 20 input and textarea fields. As a user, how can I enter information in Kannda or any other local language? https://i.stack.imgur.com/60PVT.png ...

Prevent modal from closing when the button is clicked in Semantic-ui React shorthand

I am facing an issue with the code snippet provided on the documentation page for a shorthand modal. I'm trying to prevent the button from closing the modal in the actions property, but using preventDefault is not working for me. Can someone help me f ...