Skip to content

Commit

Permalink
perf: prevent unnecessary buffer copy
Browse files Browse the repository at this point in the history
closes #154
  • Loading branch information
zbjornson authored and dougwilson committed Mar 13, 2019
1 parent f6873b5 commit 7e91b18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -6,6 +6,7 @@ unreleased
- deps: mime-db@'>= 1.38.0 < 2'
* deps: on-headers@~1.0.2
- Fix `res.writeHead` patch missing return value
* perf: prevent unnecessary buffer copy

1.7.3 / 2018-07-15
==================
Expand Down
15 changes: 13 additions & 2 deletions index.js
Expand Up @@ -85,7 +85,7 @@ function compression (options) {
}

return stream
? stream.write(Buffer.from(chunk, encoding))
? stream.write(toBuffer(chunk, encoding))
: _write.call(this, chunk, encoding)
}

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

// write Buffer for Node.js 0.8
return chunk
? stream.end(Buffer.from(chunk, encoding))
? stream.end(toBuffer(chunk, encoding))
: stream.end()
}

Expand Down Expand Up @@ -275,3 +275,14 @@ function shouldTransform (req, res) {
return !cacheControl ||
!cacheControlNoTransformRegExp.test(cacheControl)
}

/**
* Coerce arguments to Buffer
* @private
*/

function toBuffer (chunk, encoding) {
return !Buffer.isBuffer(chunk)
? Buffer.from(chunk, encoding)
: chunk
}

0 comments on commit 7e91b18

Please sign in to comment.