C
C
Caretaker2019-03-11 16:02:50
Node.js
Caretaker, 2019-03-11 16:02:50

How memory-clogging is "const self = this" in class methods?

Greetings. Need help from experts!
I will try to describe request() using a simplified example, because I don’t know how easy it is to describe in words =
) this is still only an algorithmic functional view.
Functional usage:

let varName;
function result(body, ext) {
  varName = ext;
  ...
};
function req(params, ext) {
  request(params, (err, req, body) => {
    result(body, ext);
  });
};

Usage in class form:
class CLS {
  constructor() {
    this.varName = null;
  };
  ...
  result(body, ext) {
    this.varName = ext;
    ...
  };

  req(params, ext) {
    const self = this;
    request(params, (err, req, body) => {
      self.result(body, ext);
    });
  };
  ...
};

The use of classes is more justified, because requires inheritance + class properties (several objects of the same class, each with its own state and data). However, given that quite a few callback methods will be required, it is confusing to need to use the const self = this;
How much memory will it consume, given that the calls to these methods will be constant ... Will it not turn out that because of these self - s there will be memory loss with all the bad consequences?!
The "try and see" option is the best, of course, but changing from a class style to a procedural style can be a huge problem later on.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Yarkov, 2019-03-11
@zuart

const self = this;
request(params, (err, req, body) => {
      self.result(body, ext);
});

Why is self here if you have an arrow function?

K
Konstantin Kitmanov, 2019-03-11
@k12th

Task to think about:

class A {
    answer: null;
    constructor(answer) {
        this.answer = answer;
    }
}
const a = new A(42);
const b = a;
b.answer = 24;
console.log(a.answer); // что выведет? почему? сколько в памяти содержится экземпляров класса A?

Well, in general, Aleksey Yarkov wrote correctly, you have an arrow function there and you need self like an umbrella for a fish.

R
Robur, 2019-03-11
@Robur

Even if you remove self, you will create a new function each time, and it will take much more memory.
You don't worry about that. On the one hand, you were correctly told that you should not worry about self, on the other hand, this does not mean that you should not worry at all. And it does not depend on how you write the code - with or without classes. in a functional style, you can easily use up memory at times without noticing it anymore.
If, in essence, the example - you will have self in the closure, it will remain there as long as there is a live link to this closure somewhere, that is, as long as there is a link to the function that you pass to helper.method somewhere. as soon as the garbage collector can collect it all, the memory will be freed. If references don't die and you call this function over and over, memory will run out even if you remove self altogether.
It's important to make sure you don't have memory leaks anywhere. If the code is such that it must work long, hard and well, for example, large and complex mathematical calculations, or real time is important (so that the garbage collector does not suddenly pause everything), then special techniques are needed to avoid creating any new object in memory to the maximum.
Read about the garbage collector, how it works and where memory leaks come from in node.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question