Skip to content

Commit

Permalink
fix benchmark Custom Request
Browse files Browse the repository at this point in the history
  • Loading branch information
sirenkovladd committed Aug 31, 2023
1 parent 700d373 commit 757708a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
30 changes: 10 additions & 20 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const { Readable, addAbortSignal } = require('stream')
const cookie = require('cookie')
const util = require('util')
const assert = require('assert')
const warning = require('process-warning')()

Expand Down Expand Up @@ -214,31 +213,22 @@ class Request extends Readable {
}
}

function Classes (bases) {
class _CustomLMRRequest {
constructor (...opt) {
bases.forEach(Base => {
Object.assign(this, new Base(...opt))
})

util.inherits(this.constructor, bases[bases.length - 1])
}
}
bases.forEach(base => {
Object.getOwnPropertyNames(base.prototype)
.filter(prop => prop !== 'constructor')
.forEach(prop => { _CustomLMRRequest.prototype[prop] = base.prototype[prop] })
})
return _CustomLMRRequest
}

/**
* @template T
* @param {new (opt: import('../types').InjectOptions) => T} CustomRequest
* @returns {new (opt: import('../types').InjectOptions) => T & Request}
*/
function getCustomRequest (CustomRequest) {
return Classes([Request, CustomRequest])
class _CustomLMRRequest extends CustomRequest {
constructor (...opt) {
super(...opt)
Object.assign(this, new Request(...opt))
}
}
Object.getOwnPropertyNames(Request.prototype)
.filter(prop => prop !== 'constructor')
.forEach(prop => { _CustomLMRRequest.prototype[prop] = Request.prototype[prop] })
return _CustomLMRRequest
}

module.exports = Request
Expand Down
10 changes: 6 additions & 4 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockCustomReq = {
},
Request: http.IncomingMessage
}

const mockReqCookies = {
url: 'http://localhost',
method: 'GET',
Expand All @@ -52,11 +53,12 @@ const mockReqCookiesPayload = {
}
}

suite.add('Request', function () {
new Request(mockReq)
})
suite
.add('Request', function () {
new Request(mockReq)
})
.add('Custom Request', function () {
new Request.CustomRequest(mockCustomReq)
new (Request.getCustomRequest(mockCustomReq.Request))(mockCustomReq)
})
.add('Request With Cookies', function () {
new Request(mockReqCookies)
Expand Down

0 comments on commit 757708a

Please sign in to comment.