From 95966fa0d47eee3c503e8943472ecf0dd2518ff4 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 22 Aug 2022 16:24:34 +0300 Subject: [PATCH] perf: hexToBase64: use preallocated buffer --- .../src/platform/node/hex-to-base64.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/platform/node/hex-to-base64.ts b/packages/opentelemetry-core/src/platform/node/hex-to-base64.ts index 31fd1890c1..66d4ad0fe9 100644 --- a/packages/opentelemetry-core/src/platform/node/hex-to-base64.ts +++ b/packages/opentelemetry-core/src/platform/node/hex-to-base64.ts @@ -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) {