Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(fetch): improve body mixin methods #3109

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions benchmarks/fetch/body-arraybuffer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { group, bench, run } from 'mitata'
import { Response } from '../../lib/web/fetch/response.js'

const settings = {
small: 2 << 8,
middle: 2 << 12,
long: 2 << 16
}

for (const [name, length] of Object.entries(settings)) {
const buffer = Buffer.allocUnsafe(length).map(() => (Math.random() * 100) | 0)
group(`${name} (length ${length})`, () => {
bench('Response#arrayBuffer', async () => {
return await new Response(buffer).arrayBuffer()
})

// for comparison
bench('Response#text', async () => {
return await new Response(buffer).text()
})
})
}

await run()
5 changes: 3 additions & 2 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ function bodyMixinMethods (instance) {
// given a byte sequence bytes: return a new ArrayBuffer
// whose contents are bytes.
return consumeBody(this, (bytes) => {
return new Uint8Array(bytes).buffer
// Note: arrayBuffer already cloned.
return bytes.buffer
tsctx marked this conversation as resolved.
Show resolved Hide resolved
}, instance)
},

Expand Down Expand Up @@ -432,7 +433,7 @@ async function consumeBody (object, convertBytesToJSValue, instance) {
// 5. If object’s body is null, then run successSteps with an
// empty byte sequence.
if (object[kState].body == null) {
successSteps(new Uint8Array())
successSteps(Buffer.allocUnsafe(0))
return promise.promise
}

Expand Down
7 changes: 5 additions & 2 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,8 +1069,7 @@ async function fullyReadBody (body, processBody, processBodyError) {

// 5. Read all bytes from reader, given successSteps and errorSteps.
try {
const result = await readAllBytes(reader)
successSteps(result)
successSteps(await readAllBytes(reader))
} catch (e) {
errorSteps(e)
}
Expand Down Expand Up @@ -1128,6 +1127,10 @@ async function readAllBytes (reader) {

if (done) {
// 1. Call successSteps with bytes.
if (bytes.length === 1) {
const { buffer, byteOffset, byteLength } = bytes[0]
return Buffer.from(buffer.slice(byteOffset, byteOffset + byteLength), 0, byteLength)
}
return Buffer.concat(bytes, byteLength)
}

Expand Down
Loading