Skip to content

Commit 7e91b18

Browse files
zbjornsondougwilson
authored andcommitted
perf: prevent unnecessary buffer copy
closes #154
1 parent f6873b5 commit 7e91b18

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ unreleased
66
- deps: mime-db@'>= 1.38.0 < 2'
77
* deps: on-headers@~1.0.2
88
- Fix `res.writeHead` patch missing return value
9+
* perf: prevent unnecessary buffer copy
910

1011
1.7.3 / 2018-07-15
1112
==================

index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function compression (options) {
8585
}
8686

8787
return stream
88-
? stream.write(Buffer.from(chunk, encoding))
88+
? stream.write(toBuffer(chunk, encoding))
8989
: _write.call(this, chunk, encoding)
9090
}
9191

@@ -112,7 +112,7 @@ function compression (options) {
112112

113113
// write Buffer for Node.js 0.8
114114
return chunk
115-
? stream.end(Buffer.from(chunk, encoding))
115+
? stream.end(toBuffer(chunk, encoding))
116116
: stream.end()
117117
}
118118

@@ -275,3 +275,14 @@ function shouldTransform (req, res) {
275275
return !cacheControl ||
276276
!cacheControlNoTransformRegExp.test(cacheControl)
277277
}
278+
279+
/**
280+
* Coerce arguments to Buffer
281+
* @private
282+
*/
283+
284+
function toBuffer (chunk, encoding) {
285+
return !Buffer.isBuffer(chunk)
286+
? Buffer.from(chunk, encoding)
287+
: chunk
288+
}

0 commit comments

Comments
 (0)