Maximizing Memory Usage in Objects

Which of these coding patterns is considered to be more efficient?

Pattern 1:

var jso = new Class({
   initialize: function () {
     // implementation
   },
   hiliteField: function () {
     // implementation
   }
});

Pattern 2:

var jso = new Class({
   initialize: function () {
     this.hiliteField = hiliteField.bind(this);
     // implementation  
   }
});
var hiliteField = function () {
   // implementation
}

Answer №1

It is my opinion that having multiple external methods up the scope chain, rather than behind the class as 'namespaced' methods, is preferable.

I have several reasons for disliking pattern #2:

  • Manageability - It will be difficult to bind a reference to each external function into the class.
  • Readability - Indicating which function is being used by what class will be a challenging task.
  • Extendibility - Classes that extend jso or use it as a mixin will not be able to access the external methods since they are outside of the jso class prototype.

However, from a memory perspective - conducting a test may confirm this - I believe that pattern 1 would likely have a smaller overhead because it is defined once on the prototype rather than in every instance individually.

Answer №2

Pattern 2 is more memory-intensive! It's all about delving into the world of prototypal inheritance

What makes Pattern 2 memory-heavy? In Pattern 1, each instance of jso will point to jso.prototype.hiliteField, rather than having its own copy of hiliteField. This means that hiliteField only exists in jso.prototype.

On the other hand, in Pattern 2, a new function wrapper is created every time you assign hiliteField.bind(this);. While the additional memory usage may not be substantial, it does lead to slower performance, especially during Class initialization.

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

Is it considered poor form to send as many as 100 ajax requests when loading a webpage?

My table can display up to 100 rows, sometimes even more. Is it considered a bad practice to loop through all these rows and send an AJAX post request to fetch data? I am hesitant to do this during the page load as it may significantly slow down the loadin ...

Documentation for Lambda function within an object

Looking to properly document the sock and data variables using JSDoc in my code. var exec = { /** * @param {Number} sock * @param {String} data */ 1: (sock, data) => { console.log("GG"); }, 2: (sock, data ...

Arrange the array in ascending order based on a specific value

Within my array, I have various objects of different types: myArray = [ {state: "enabled", name: "zName"}, {state: "trying", name: "aName2"}, {state: "disabled", name: "aName3"}, {state: "dis ...

What is causing my Node JS app to only show the initial JSON object?

Having an issue with my Node application while writing a JSON object and integrating the Twilio API. The console log displays all objects properly, but when written to the document, only the first object is visible. Can anyone help me figure out why this d ...

Is it possible to run concurrent PostgreSQL queries in NodeJS?

I'm unsure why, but the task is supposed to be run in parallel and should only take 2 seconds: const test = async () => { client.query("SELECT pg_sleep(2) FROM test", (err, result) => { console.log("DONE!"); }) client.query("SELECT pg ...

Creating a Discord Bot: Building an Embed Table/List with Segmented Sections

Currently in the process of learning Javascript and venturing into discord.js, I'm fully aware that the code I am working with is not up to par and requires some serious refinements. The main objective here is to split up the arguments of a command a ...

Tips for setting up nested folders using Node.js on a Windows machine:

Is there a way to use Nodejs in Windows 10/11 to create a parent folder and then add a new folder inside of that parent folder, like this: parent/child within the Documents folder? ...

Error: The function (0 , _testUtils.createLocalVue) is not defined as a function

I need help troubleshooting my code. I am trying to test my Vue frontend using jest, but I keep getting the error message "TypeError: (0 , _testUtils.createLocalVue) is not a function" specifically on the line const localVue = createLocalVue(); import { ...

Scroll the page only when the first radio button is clicked

After selecting a radio button with the ID "yourgoal", a div is displayed on my page. I want the page to scroll to that div only the first time a button in "yourgoal" is clicked. Below is the code I am currently using. However, I do not want the page to sc ...

Reinitializing the database with Cypress (postgresql)

When populating the database, I intentionally do it partially for certain tests. However, after completing all tests, I would like to reset the database in order to avoid any complications that may arise during the next populate process. How can I efficien ...

The image will remain static and will not alternate between hidden and visible states

I am facing a challenge trying to toggle an image between 'hidden' and 'show' My approach is influenced by the post on How to create a hidden <img> in JavaScript? I have implemented two different buttons, one using html and the ...

Guide on displaying the name attribute of a button along with its price in a separate div when clicked

Upon clicking the Koala button, I want to display a message showing the id name of the button and the corresponding price for Koalas. Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link h ...

Exploring the architecture of Redux-thunk and validating the impact of side effects

I've been using redux-thunk and I'm not entirely sure if my side effects (specifically the showAlertError function) are structured properly. While my jest test setup seems to be fine at first glance, I encountered an error: jest.fn() value mus ...

Manipulating the visibility of components by toggling on click events according to the existing state in ReactJS

Disclosure: I am still getting familiar with ReactJS I'm currently working on incorporating a dialog window for selecting a country/language on the initial page of my application. Here's the concept: There's a small flag button in the to ...

Creating a responsive modal in Bootstrap 4

Everything seems to be in order, but my JavaScript doesn't seem to be functioning. I've implemented the code for a bootstrap 4 modal below: <div id="qmsManual" tabindex="-1" role="dialog" aria-labelledby="qmsManualModal" aria-hidden="true" c ...

Implementing the Context API with React Router: using version '^5.2.0' of react-router-dom and version '17.0.2' of React

I am currently struggling to retrieve the user value from my Header component using the Context API, but unfortunately it is returning as undefined. Below is my code snippet. import React, { createContext, useEffect, useState } from "react"; exp ...

PHP jQuery buttons for popovers

With a simple click of a button, I can generate 8 presentations and then edit each one individually by clicking on its respective name. Within this editing process, there is another form where I would like to include additional buttons that allow me to cus ...

How to transform a JQuery button into a dropdown menu

For inspiration, consider this JQuery UI Button example: http://jqueryui.com/demos/button/#splitbutton Now, how can you create a dropdown menu when the small button is clicked? My main concern is the way .button() alters the button's actual appearanc ...

Guide to assigning unique identifiers to all elements within an array using JavaScript

I have an array of objects with numeric keys that correspond to specific data values. I am attempting to restructure this object in a way that includes an 'id' field for each entry. Here is the original object: [ { "1": "data1", "5": "d ...

A guide on transforming JSON data into HTML using Rails

I'm struggling with parsing JSON on a webpage. My webpage is designed with circles, where clicking on a circle triggers a call to the controller to retrieve specific information from a database and display it as graphs and text. The issue I'm fa ...