diff options
author | Marcos Casagrande <marcos@denode.com> | 2023-09-22 04:06:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-21 20:06:42 -0600 |
commit | 65a94a6176e3a76ca52d0666cf882689ac0b5b9c (patch) | |
tree | 745255a0ed69773f3118087b67d7137604c131f9 /ext/fetch/23_request.js | |
parent | 142449ecab20006c5cfd15462814650596bc034d (diff) |
perf(ext/fetch): use new instead of createBranded (#20624)
This PR optimizes `fromInner*` methods of `Request` / `Header` /
`Response` used by `Deno.serve` and `fetch` by using `new` instead of
`ObjectCreate` from `createBranded`.
The "brand" is created by passing `webidl.brand` to the constructor
instead.
https://github.com/denoland/deno/blob/142449ecab20006c5cfd15462814650596bc034d/ext/webidl/00_webidl.js#L1001-L1005
### Benchmark
```js
const createBranded = Symbol("create branded");
const brand = Symbol("brand");
class B {
constructor(init) {
if (init === createBranded) {
this[brand] = brand;
}
}
}
Deno.bench("Object.create(protoype)", () => {
Object.create(B.prototype);
});
Deno.bench("new Class", () => {
new B(createBranded);
});
```
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.37.0 (x86_64-unknown-linux-gnu)
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
Object.create(protoype) 8.74 ns/iter 114,363,610.3 (7.32 ns … 26.02 ns) 8.65 ns 13.39 ns 14.47 ns
new Class 3.05 ns/iter 328,271,012.2 (2.78 ns … 9.1 ns) 3.06 ns 3.46 ns 3.5 ns
```
Diffstat (limited to 'ext/fetch/23_request.js')
-rw-r--r-- | ext/fetch/23_request.js | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/ext/fetch/23_request.js b/ext/fetch/23_request.js index 5232cc13c..c09bd4880 100644 --- a/ext/fetch/23_request.js +++ b/ext/fetch/23_request.js @@ -52,6 +52,7 @@ const _mimeType = Symbol("mime type"); const _body = Symbol("body"); const _url = Symbol("url"); const _method = Symbol("method"); +const _brand = webidl.brand; /** * @param {(() => string)[]} urlList @@ -275,6 +276,11 @@ class Request { * @param {RequestInit} init */ constructor(input, init = {}) { + if (input === _brand) { + this[_brand] = _brand; + return; + } + const prefix = "Failed to construct 'Request'"; webidl.requiredArguments(arguments.length, 1, prefix); input = webidl.converters["RequestInfo_DOMString"]( @@ -284,7 +290,7 @@ class Request { ); init = webidl.converters["RequestInit"](init, prefix, "Argument 2"); - this[webidl.brand] = webidl.brand; + this[_brand] = _brand; /** @type {InnerRequest} */ let request; @@ -554,7 +560,7 @@ function toInnerRequest(request) { * @returns {Request} */ function fromInnerRequest(inner, signal, guard) { - const request = webidl.createBranded(Request); + const request = new Request(_brand); request[_request] = inner; request[_signal] = signal; request[_getHeaders] = () => headersFromHeaderList(inner.headerList, guard); |