From e23f3376352f87eb6770ea76e293ff5289de8765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 8 Mar 2017 15:02:34 +0100 Subject: [PATCH] improve performance for large files Using `buf = Buffer.concat([buf, data]);` on the data event copies the data in a new larger buffer everytime. Instead, store the intermediate small buffers in an array and concat only at the end. --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index e93291d..c34d443 100644 --- a/lib/index.js +++ b/lib/index.js @@ -67,11 +67,11 @@ function processMultipart(options, req, res, next) { // Build req.files fields busboy.on('file', function(fieldname, file, filename, encoding, mime) { - let buf = new Buffer(0); + const buffers = []; let safeFileNameRegex = /[^\w-]/g; file.on('data', function(data) { - buf = Buffer.concat([buf, data]); + buffers.push(data); if (options.debug) return console.log('Uploading %s -> %s', fieldname, filename); @@ -81,6 +81,7 @@ function processMultipart(options, req, res, next) { if (!req.files) req.files = {}; + const buf = Buffer.concat(buffers); // see: https://github.com/richardgirges/express-fileupload/issues/14 // firefox uploads empty file in case of cache miss when f5ing page. // resulting in unexpected behavior. if there is no file data, the file is invalid.