Skip to content

Commit

Permalink
perf: use TextEncoder instead of charCodeAt loop
Browse files Browse the repository at this point in the history
Use `TypedArray` directly without constructing a new `Blob`.
  • Loading branch information
ACTCD committed Sep 5, 2024
1 parent 0882bb9 commit 1ef027b
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/ext/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,11 @@ async function handleMessage(message, sender) {
const user = details.user || null;
const password = details.password || null;
let body = details.data || null;
// deprecate once body supports more data types
// the `binary` key will no longer needed
if (typeof body === "string" && details.binary) {
const len = body.length;
const arr = new Uint8Array(len);
for (let i = 0; i < len; i++) {
arr[i] = body.charCodeAt(i);
}
body = new Blob([arr], { type: "text/plain" });
body = new TextEncoder().encode(body);
}

// xhr instances automatically filter out unexpected user values
xhr.timeout = details.timeout;
xhr.responseType = details.responseType;
Expand Down

0 comments on commit 1ef027b

Please sign in to comment.