Skip to content

Commit 599e95a

Browse files
authored
Increase performance of body length calculation for larger payloads on browser (#1088)
* Increase performance of body length calculation for larger payloads on browser * Create changeset
1 parent 44f78bd commit 599e95a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

.changeset/silent-waves-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/util-body-length-browser": patch
3+
---
4+
5+
Use TextEncoder to calculate body length on browsers (where available)

packages/util-body-length-browser/src/calculateBodyLength.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
const TEXT_ENCODER = typeof TextEncoder == "function" ? new TextEncoder() : null;
2+
13
/**
24
* @internal
35
*/
46
export const calculateBodyLength = (body: any): number | undefined => {
57
if (typeof body === "string") {
8+
if (TEXT_ENCODER) {
9+
return TEXT_ENCODER.encode(body).byteLength;
10+
}
11+
612
let len = body.length;
713

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

0 commit comments

Comments
 (0)