Skip to content

Commit

Permalink
perf: hexToBase64: use preallocated buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Aug 22, 2022
1 parent 2d17e04 commit 95966fa
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/opentelemetry-core/src/platform/node/hex-to-base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ function intValue(charCode: number): number {
return charCode - 55;
}

const buf8 = Buffer.alloc(8);
const buf16 = Buffer.alloc(16);

export function hexToBase64(hexStr: string): string {
const buf = Buffer.alloc(hexStr.length / 2);
let buf;
if (hexStr.length === 16) {
buf = buf8;
} else if (hexStr.length === 32) {
buf = buf16;
} else {
buf = Buffer.alloc(hexStr.length / 2);
}
let offset = 0;

for (let i = 0; i < hexStr.length; i += 2) {
Expand Down

0 comments on commit 95966fa

Please sign in to comment.