Skip to content

Commit ffcffb2

Browse files
EommUzlopak
andauthoredSep 19, 2023
chore: more perf (#5016)
* chore: genId perf optimization * chore: perf syntax * chore: perf syntax reply * chore: Update lib/reply.js Co-authored-by: Uzlopak <aras.abbasi@googlemail.com> * rollback --------- Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
1 parent cc6c04e commit ffcffb2

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed
 

‎lib/reply.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Reply.prototype.header = function (key, value = '') {
244244
}
245245

246246
if (Array.isArray(value)) {
247-
this[kReplyHeaders][key].push(...value)
247+
Array.prototype.push.apply(this[kReplyHeaders][key], value)
248248
} else {
249249
this[kReplyHeaders][key].push(value)
250250
}
@@ -810,7 +810,7 @@ function onResponseCallback (err, request, reply) {
810810
}
811811

812812
function buildReply (R) {
813-
const props = [...R.props]
813+
const props = R.props.slice()
814814

815815
function _Reply (res, request, log) {
816816
this.raw = res

‎lib/reqIdGenFactory.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
* @returns {GenerateRequestId}
1313
*/
1414
function reqIdGenFactory (requestIdHeader, optGenReqId) {
15+
const genReqId = optGenReqId || buildDefaultGenReqId()
16+
17+
if (requestIdHeader) {
18+
return buildOptionalHeaderReqId(requestIdHeader, genReqId)
19+
}
20+
21+
return genReqId
22+
}
23+
24+
function buildDefaultGenReqId () {
1525
// 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
1626
// With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
1727
// This is very likely to happen in real-world applications, hence the limit is enforced.
@@ -20,20 +30,16 @@ function reqIdGenFactory (requestIdHeader, optGenReqId) {
2030
const maxInt = 2147483647
2131

2232
let nextReqId = 0
23-
function defaultGenReqId (_req) {
33+
return function defaultGenReqId () {
2434
nextReqId = (nextReqId + 1) & maxInt
2535
return `req-${nextReqId.toString(36)}`
2636
}
37+
}
2738

28-
const genReqId = optGenReqId || defaultGenReqId
29-
30-
if (requestIdHeader) {
31-
return function (req) {
32-
return req.headers[requestIdHeader] || genReqId(req)
33-
}
39+
function buildOptionalHeaderReqId (requestIdHeader, genReqId) {
40+
return function (req) {
41+
return req.headers[requestIdHeader] || genReqId(req)
3442
}
35-
36-
return genReqId
3743
}
3844

3945
module.exports = {

‎lib/request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function buildRequest (R, trustProxy) {
6666
}
6767

6868
function buildRegularRequest (R) {
69-
const props = [...R.props]
69+
const props = R.props.slice()
7070
function _Request (id, params, req, query, log, context) {
7171
this.id = id
7272
this[kRouteContext] = context

0 commit comments

Comments
 (0)
Please sign in to comment.