diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2023-08-10 19:41:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-10 19:41:09 +0200 |
commit | 2d3d0a579d8c3c5f038470e2c6610f6d44d29eb0 (patch) | |
tree | 0c6585c4deb4e0bd5f1a19d6cdd3e8ee234dd505 /ext/fetch/20_headers.js | |
parent | 94d664535bccdd2a45b393f819cbf601890492cf (diff) |
perf(ext/headers): optimize getHeader using for loop (#20115)
This PR optimizes the `getHeader` function by replacing `.filter` and
`.map` with a `for` loop
**this patch**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.0 (x86_64-unknown-linux-gnu)
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------- -----------------------------
Headers.get 132.2 ns/iter 7,564,093.4 (125.81 ns … 147.66 ns) 133.79 ns 144.92 ns 145.36 ns
```
**main**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.0 (x86_64-unknown-linux-gnu)
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------- -----------------------------
Headers.get 191.48 ns/iter 5,222,523.6 (182.75 ns … 212.22 ns) 193.5 ns 205.96 ns 211.51 ns
```
```js
const headers = new Headers({
"Content-Type": "application/json",
"Date": "Thu, 10 Aug 2023 07:45:10 GMT",
"X-Deno": "Deno",
"Powered-By": "Deno",
"Content-Encoding": "gzip",
"Set-Cookie": "__Secure-ID=123; Secure; Domain=example.com",
"Content-Length": "150",
"Vary": "Accept-Encoding, Accept, X-Requested-With",
});
Deno.bench("Headers.get", () => {
const i = headers.get("x-deno");
});
```
Diffstat (limited to 'ext/fetch/20_headers.js')
-rw-r--r-- | ext/fetch/20_headers.js | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/ext/fetch/20_headers.js b/ext/fetch/20_headers.js index 45bd29ad3..fabd39c0e 100644 --- a/ext/fetch/20_headers.js +++ b/ext/fetch/20_headers.js @@ -22,12 +22,10 @@ import { const primordials = globalThis.__bootstrap.primordials; const { ArrayIsArray, - ArrayPrototypeMap, ArrayPrototypePush, ArrayPrototypeSort, ArrayPrototypeJoin, ArrayPrototypeSplice, - ArrayPrototypeFilter, ObjectEntries, ObjectHasOwn, RegExpPrototypeExec, @@ -162,13 +160,13 @@ function appendHeader(headers, name, value) { */ function getHeader(list, name) { const lowercaseName = byteLowerCase(name); - const entries = ArrayPrototypeMap( - ArrayPrototypeFilter( - list, - (entry) => byteLowerCase(entry[0]) === lowercaseName, - ), - (entry) => entry[1], - ); + const entries = []; + for (let i = 0; i < list.length; i++) { + if (byteLowerCase(list[i][0]) === lowercaseName) { + ArrayPrototypePush(entries, list[i][1]); + } + } + if (entries.length === 0) { return null; } else { |