I've Node application which is getting zip file from rest API request. I use the following code for file in size 2MB and it takes almost 10 sec , There is a way to improve performance here ?
The file should be extracted and saved in local system...
This codes works fine except the performance...
`1. if the request should invoked from client and its take to long when I should send the response? if I'll wait until the process finish this can take 10 sec...
2.Should I use promise in this case if yes can you provide example please with the following code(blue bird,q...)
3.there is a way to optimize somehow the performance
var unzip = require('unzip');
app.post('/', function(req, res) {
var ext = unzip.Extract({
path: 'C://myFolder'
}).on('close', function() {
res.sendStatus(200);
}).on('error', function(err) {
// res.sendStatus(500);
});
req.pipe(ext);
});
The code isn't the problem, when I run the exact code it takes 750ms to extract a 7.5MB ZIP file. I used this to time it:
app.post('/', function(req, res) {
console.time('unzip');
var ext = unzip.Extract({ path: ... }).on('close', function() {
console.timeEnd('unzip');
res.sendStatus(200);
}).on('error', function(err) {
console.timeEnd('unzip');
res.sendStatus(500);
});
req.pipe(ext);
});
Uploading the file:
$ ls -al test.zip
-rw-rw-r-- 1 robert wheel 7553635 Jul 17 09:31 test.zip
$ curl -XPOST localhost:3012 --data-binary @test.zip
This is on a Macbook Pro.
You don't say how exactly you're timing the code, but my guess is that it takes 10 seconds between the client starting the upload to until it receives a response, which introduces a lot of extra variables (speed of upload, network connection, particular middleware in your Express app, etc).
Run the extraction code completely separate from Express to see if that really is the bottleneck. Here's a simple standalone script to test with:
var fs = require('fs');
var unzip = require('unzip');
console.time('unzip');
fs.createReadStream('test.zip').pipe(unzip.Extract({ path : ... })).on('close', function() {
console.timeEnd('unzip');
});
If that takes 10 seconds for a 2MB file, I would think that your hardware is just wildly underpowered, which can't be solved from Node.js.