Deno vs Node.js: Performance comparison for file server
From v1.25, Deno has introduced a totally new HTTP server called flash. The flash server is still under the unstable umbrella. The flash server is limited to HTTP/1.1 only. There is no HTTP/2 support yet. In this comparison, we’ll find out how the Deno’s new HTTP server stands in front of Node’s HTTP server when serving files.
Test setup
Environment
The performance comparison is executed on the following environment:
- System: MacBook M1 with 16G of memory
- Local: The SUT and tester are on the same system
- Deno version: 1.25.0
- Node version: 18.8.0
Code
The following is the code for the file server in both the runtimes. In both cases, only native APIs are used.
Node.js
const http = require("http");
const { stat } = require("fs").promises;
const fs = require("fs");const baseServePath = "./testdata";
const server = http.createServer(
async (req, res) => {
const filePath = baseServePath + req.url;
const fileSize = (await stat(filePath)).size;
res.writeHead(200, {
"content-type": "application/pdf",
"content-length": fileSize,
});
fs.createReadStream(
filePath,
).pipe(res);
},
);server.listen(3000);
In Node.js, we need to run a stat call to set the ‘content-length’ header.
Deno
const baseServePath = "./testdata";Deno.serve(async (req) => {
const filePath = baseServePath + (new URL(req.url)).pathname;
const file = await Deno.open(filePath);
return new Response(file.readable, {
headers: {
"content-type": "application/pdf",
},
});
}, { port: 3000 });
In Deno, the content-length header is set by the new server. Therefore, no stat call is required.
Settings
The performance test is executed in following configurations:
- 10 concurrent connections to run a total of 10K requests
- 25 concurrent connections to run a total of 10K requests
- 50 concurrent connections to run a total of 10K requests
Measurements
The following measurements are recorded for each test configuration:
- Time taken
- Requests per second
- Minimum
- 1st Quartile
- Median
- Mean
- 3rd Quartile
- Maximum
- CPU usage
- Memory usage
Results
A total of 10K requests are executed for each test configuration.
For ease of comparison, all the results are summarized graphically. Here are the 16 graphs covering each of the above measurement:
Conclusion
The Node.js’s file serving seems slower compared to Deno. Also, Node.js uses more CPU and memory.