Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace setHeaders function with optimized inline header setting #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==================

* refactor: replace setHeaders function with optimized inline header setting
* remove unnecessary devDependency `safe-buffer`
* remove `unpipe` package and use native `unpipe()` method
* remove unnecessary devDependency `readable-stream`
Expand Down
24 changes: 3 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ function send (req, res, status, headers, message) {
res.removeHeader('Content-Range')

// response headers
setHeaders(res, headers)
for (const [key, value] of Object.entries(headers ?? {})) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to do some bench-marking on this. I don't want to armchair micro-benchmark on this, but I am pretty sure that this is making a unnecessary array per header and may be slower than the array from. Either way, these are the kind of changes that I want us to have a easy and reliable way to truly test the perf of before landing..

res.setHeader(key, value)
}

// security headers
res.setHeader('Content-Security-Policy', "default-src 'none'")
Expand Down Expand Up @@ -318,23 +320,3 @@ function send (req, res, status, headers, message) {
onFinished(req, write)
req.resume()
}

/**
* Set response headers from an object.
*
* @param {OutgoingMessage} res
* @param {object} headers
* @private
*/

function setHeaders (res, headers) {
if (!headers) {
return
}

var keys = Object.keys(headers)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
res.setHeader(key, headers[key])
}
}
Loading