mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 23:59:59 -05:00
perf(ext/fetch): Use the WebIDL conversion to DOMString rather than USVString for Response constructor (#12201)
This commit is contained in:
parent
09f2cdbc72
commit
b095157c1d
5 changed files with 48 additions and 6 deletions
|
@ -111,6 +111,11 @@ const EXEC_TIME_BENCHMARKS: &[(&str, &[&str], Option<i32>)] = &[
|
|||
&["run", "cli/tests/testdata/text_encoder_into_perf.js"],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"response_string",
|
||||
&["run", "cli/tests/testdata/response_string_perf.js"],
|
||||
None,
|
||||
),
|
||||
(
|
||||
"check",
|
||||
&[
|
||||
|
|
34
cli/tests/testdata/response_string_perf.js
vendored
Normal file
34
cli/tests/testdata/response_string_perf.js
vendored
Normal file
|
@ -0,0 +1,34 @@
|
|||
const mixed = "@Ā๐😀";
|
||||
|
||||
function generateRandom(bytes) {
|
||||
let result = "";
|
||||
let i = 0;
|
||||
while (i < bytes) {
|
||||
const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i));
|
||||
switch (toAdd) {
|
||||
case 0:
|
||||
result += mixed[0];
|
||||
i++;
|
||||
break;
|
||||
case 1:
|
||||
result += mixed[1];
|
||||
i++;
|
||||
break;
|
||||
case 2:
|
||||
result += mixed[2];
|
||||
i++;
|
||||
break;
|
||||
case 3:
|
||||
result += mixed[3];
|
||||
result += mixed[4];
|
||||
i += 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const randomData = generateRandom(1024);
|
||||
for (let i = 0; i < 10_000; i++) {
|
||||
new Response(randomData);
|
||||
}
|
|
@ -372,7 +372,7 @@
|
|||
return { body, contentType };
|
||||
}
|
||||
|
||||
webidl.converters["BodyInit"] = (V, opts) => {
|
||||
webidl.converters["BodyInit_DOMString"] = (V, opts) => {
|
||||
// Union for (ReadableStream or Blob or ArrayBufferView or ArrayBuffer or FormData or URLSearchParams or USVString)
|
||||
if (V instanceof ReadableStream) {
|
||||
// TODO(lucacasonato): ReadableStream is not branded
|
||||
|
@ -393,10 +393,13 @@
|
|||
return webidl.converters["ArrayBufferView"](V, opts);
|
||||
}
|
||||
}
|
||||
return webidl.converters["USVString"](V, opts);
|
||||
// BodyInit conversion is passed to extractBody(), which calls core.encode().
|
||||
// core.encode() will UTF-8 encode strings with replacement, being equivalent to the USV normalization.
|
||||
// Therefore we can convert to DOMString instead of USVString and avoid a costly redundant conversion.
|
||||
return webidl.converters["DOMString"](V, opts);
|
||||
};
|
||||
webidl.converters["BodyInit?"] = webidl.createNullableConverter(
|
||||
webidl.converters["BodyInit"],
|
||||
webidl.converters["BodyInit_DOMString?"] = webidl.createNullableConverter(
|
||||
webidl.converters["BodyInit_DOMString"],
|
||||
);
|
||||
|
||||
window.__bootstrap.fetchBody = { mixinBody, InnerBody, extractBody };
|
||||
|
|
|
@ -445,7 +445,7 @@
|
|||
{
|
||||
key: "body",
|
||||
converter: webidl.createNullableConverter(
|
||||
webidl.converters["BodyInit"],
|
||||
webidl.converters["BodyInit_DOMString"],
|
||||
),
|
||||
},
|
||||
{ key: "redirect", converter: webidl.converters["RequestRedirect"] },
|
||||
|
|
|
@ -253,7 +253,7 @@
|
|||
*/
|
||||
constructor(body = null, init = {}) {
|
||||
const prefix = "Failed to construct 'Response'";
|
||||
body = webidl.converters["BodyInit?"](body, {
|
||||
body = webidl.converters["BodyInit_DOMString?"](body, {
|
||||
prefix,
|
||||
context: "Argument 1",
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue