-
Notifications
You must be signed in to change notification settings - Fork 572
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
fetch: limit web streams usage in body mixin methods #2164
Comments
benchmarks: import { Response as UndiciResponse } from './index.js'
import { cronometro } from 'cronometro'
await cronometro({
async 'undici Response' () {
await new UndiciResponse('abc').text()
},
async 'global Response' () {
await new Response('abc').text()
}
})
|
I'm -1 for anything that goes against the spec without significant improvements. |
I understand, but 10% performance improvement is significant for the small change I made. We still extract the body (convert it to a readable web stream), but instead of reading the bytes from that stream, we use the body's source. The body is still parsed in the same way, the only thing missing is to set the body to used when consumed, which can easily be done. I also don't think the code becomes harder to read or understand, given a comment that explains its purpose. |
I believe this is not against the spec. It is just a shortcut that spec does not care about (at the moment). I think this is a clever way of improving performance without impacting the rest of the codebase. I’m definitely +1. |
I'm not blocking. Just harder to maintain when the spec changes it wording. Then you have to reverse engineering back to the old wording. |
I agree. In my experience, if we followed the spec in Ada word by word, we'll never get this fast. |
I think it's important to note the following with this change:
Regarding the latter point, there is observable behavior in the formData method in regards to how errors are thrown (either with an invalid this or if the body was already consumed) due to formData adding an extra async tick. With this change, we are simply skipping over |
I know you're not blocking the change but I typically don't like adding or changing things that are disapproved. |
How about -0 then? 🙂 |
The fetch spec requires us to create a new web Readable stream for every
Response
andRequest
object, which can likely be avoided in most cases.Given the following examples:
A web stream is created and then converted back to a string (or blob, arraybuffer, etc.)! The following diff improves
.text()
performance when the body passed is a string by 10%, as a simple demo (we could further limit web stream usage for more performance gains!!!).cc @anonrig
The text was updated successfully, but these errors were encountered: