Skip to content

Commit

Permalink
Increase performance of body length calculation for larger payloads o…
Browse files Browse the repository at this point in the history
…n browser (#1088)

* Increase performance of body length calculation for larger payloads on browser

* Create changeset
  • Loading branch information
richarddd authored Dec 1, 2023
1 parent 44f78bd commit 599e95a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-waves-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/util-body-length-browser": patch
---

Use TextEncoder to calculate body length on browsers (where available)
6 changes: 6 additions & 0 deletions packages/util-body-length-browser/src/calculateBodyLength.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const TEXT_ENCODER = typeof TextEncoder == "function" ? new TextEncoder() : null;

/**
* @internal
*/
export const calculateBodyLength = (body: any): number | undefined => {
if (typeof body === "string") {
if (TEXT_ENCODER) {
return TEXT_ENCODER.encode(body).byteLength;
}

let len = body.length;

for (let i = len - 1; i >= 0; i--) {
Expand Down

0 comments on commit 599e95a

Please sign in to comment.