Skip to content
Myr

perf(request): probe the body cache without allocating (#5176)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`#cachedBody` used `Object.keys(bodyCache)[0]` to find any already cached body, allocating a fresh array on every body read (`req.json()`, `req.text()`, ...) just to read one element. A `for ... in` loop that returns on its first iteration gives the same first-key-by-insertion-order result with no allocation. `bodyCache` is a plain object literal, so there are no enumerable prototype keys to consider.

authored by Igor Savin · committed by Yusuke Wada · parent b2070d0 · Browse files · 1 file+1 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+1 added−2 removed

  • other:+1 added−2 removed

1 file+1 added−2 removed

modified:src/request.ts+1 added-2 removed
220 unmodified lines
221 if (cachedBody) {222 return cachedBody223 }224225 const anyCachedKey = Object.keys(bodyCache)[0]226 if (anyCachedKey) {225 for (const anyCachedKey in bodyCache) {226 return (bodyCache[anyCachedKey as keyof Body] as Promise<BodyInit>).then((body) => {227 if (anyCachedKey === 'json') {228 body = JSON.stringify(body)229 }
280 unmodified lines