J
J
JackShcherbakov2019-03-31 14:09:40
Node.js
JackShcherbakov, 2019-03-31 14:09:40

Do threads save memory?

Hello!
I watched a screencast on nodejs. There, the author clearly states the following position:


It is better to use streams as they save memory.

I agree with him. If we are talking about writing to the response stream (res), then everything is clear. We are gradually sending the answer in parts, we sent several parts - the buffer was cleared. And so in a circle.
When reading, everything is also clear. We read the file piece by piece.
But where does the memory saving happen in this code:
// ...
      var body = '';

      req
        .on('readable', function() {
          body += req.read();

          if (body.length > 1e4) {
            res.statusCode = 413;
            res.end("Your message is too big for my little chat");
          }
        })
// ...

We just take and shove all the contents into one variable. So why is there a stream?
Thanks in advance to everyone who helps!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question