Skip to content

Commit 7a8b2d7

Browse files
committed
perf: reuse http req option object
1 parent 10c30f8 commit 7a8b2d7

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
} = http2.constants
2323

2424
const NODE_VER = process.version.match(/v(\d+).(\d+).(\d+)(?:-(.*))/).slice(1)
25+
const REQ_OPTIONS = {}
2526

2627
if (NODE_VER[0] < 9 && (NODE_VER[0] !== 8 || NODE_VER[1] > 4)) {
2728
throw new Error(`unsupported node version (${process.version} < 8.5.0)`)
@@ -97,24 +98,23 @@ function impl (req, resOrSocket, headOrNil, {
9798
}
9899
}
99100

100-
const options = {
101-
method: req.method,
102-
hostname,
103-
port,
104-
path: req.url,
105-
headers,
106-
timeout: proxyTimeout
107-
}
101+
REQ_OPTIONS.method = req.method
102+
REQ_OPTIONS.hostname = hostname
103+
REQ_OPTIONS.port = port
104+
REQ_OPTIONS.path = req.url
105+
REQ_OPTIONS.headers = headers
106+
REQ_OPTIONS.timeout = proxyTimeout
108107

109108
if (onReq) {
110-
onReq(req, options)
109+
onReq(req, REQ_OPTIONS)
111110
}
112111

113-
proxy(req, resOrSocket, options, onRes, errorHandler)
112+
const proxyReq = http.request(REQ_OPTIONS)
113+
114+
proxy(req, resOrSocket, proxyReq, onRes, errorHandler)
114115
}
115116

116-
function proxy (req, resOrSocket, options, onRes, errorHandler) {
117-
const proxyReq = http.request(options)
117+
function proxy (req, resOrSocket, proxyReq, onRes, errorHandler) {
118118
const proxyErrorHandler = ProxyErrorHandler.create(req, proxyReq, errorHandler)
119119

120120
req

0 commit comments

Comments
 (0)