C
C
Caretaker2019-04-24 18:46:31
Node.js
Caretaker, 2019-04-24 18:46:31

Callback / Promises / await - which is faster and more economical?

Greetings.
Promises appeared with ES6, async / await were added to ES7 - recommendations are published everywhere to refuse callbacks. this is clearer and syntactically "more correct", but at the same time, contradictory data on the principles of operation (in general, they write that promises and async/await are "sugar").
In the current project, it so happens that syntactically nesting more than 2 levels of callbacks is not and will not be, however, there are several modules, the requirements for which are very "cruel" in terms of speed and memory costs. And first of all, this is due to "closures" and, accordingly, dynamic memory allocation / its cleaning + creation / deletion of the handler functions themselves. Objects are quite "heavy" in places and I really want to avoid unnecessary operations as much as possible.
Accordingly, the question is - what is still more correct to use in such conditions (after all, if promises and async/await are only "sugar", then this "sucrose" is also added in addition to the overhead from callbacks)? And why?
And yes, by the way, the situation is such that all this goodness is framed in classes and there is a “condition” for an unpredictable number of repetitions, something like (in the async / await syntax - it’s really as clear as possible if you post the current version on callbacks, there will be a long and difficult code):

class a {
  async updateMovies() {
    let v = ....;
    while (await a(v)) {
      ... тут повторяемый код метода
      ... некие операции с "v"
    };
    ... тут код завершения метода
  };

  async a(v) {
    ...
  };
};

Answer the question

In order to leave comments, you need to log in

6 answer(s)
L
Leo Developer, 2019-04-25
@zuart

If performance is so critical for you, write on the base, i.e. on callbacks.
If the built-in array method is performance-critical for you, write an algorithm.
The callback will work faster than the promise, that's obvious. And a promise is a microtask and is asynchronous anyway, which cannot be said about the callback.

S
Sergey Gornostaev, 2019-04-24
@sergey-gornostaev

Accordingly, the question is what is still more correct to use

Something that makes code cleaner, clearer, and easier to maintain. And this is async/await, other things being equal.
Will not be added. All the sugar will dissolve when the script is parsed, it will no longer be in the AST.

A
Anton Shvets, 2019-04-24
@Xuxicheta

Yes, there is a small overhead from promises, but it’s more profitable to write understandable code and buy plus one virtual machine than to save on matches and drown in callback hell.
If you have nothing complicated there, use callbacks, it’s okay on a reasonable scale. But usually, if the requirements for speed are severe, they don’t take js, but write a compiled language like golang or write a module in C / C ++.

S
sh84, 2019-05-02
@sh84

async syntax definitely wins in terms of readability and simplicity.
Whether there will be a significant gain when using callbacks - can only be found out using performance tests and / or profiling.
In my experience, there is a 90% chance that there is no difference.

D
Dmitry Makarov, 2019-04-25
@MDiMaI666

I think the difference shouldn't be that big. try to write a section this way and that and run arbitrariness tests, it will be unambiguously clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question