B
B
bro-dev2020-04-23 20:48:16
JavaScript
bro-dev, 2020-04-23 20:48:16

Why is http server over net slower than over http?

spoiler
require('http').createServer((req, res) => {
  res.end('hello world');
}).listen(8000);


spoiler
require("net").createServer((socket) => {
  socket.setNoDelay().on("data", (dataBufered) => {
    socket.end('HTTP/1.0 200 OK\r\ncontent-length: 11\r\n\r\nhello world');
    socket.destroy();
  });
}).listen(8000);

checked with
autocannon -c 100 localhost:8000

via net
https://vk.cc/at9x4m
via http
https://vk.cc/at9x5e

That is, the difference is 10 times, although net does nothing at all, and http at least parses headers, how to speed up at least to one level .
Both work in 1 copy.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2020-04-23
@xPomaHx

Because http keeps connections, and in net you destroy the socket after each request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question