Skip to content

Commit 1be2d88

Browse files
committed
crypto: avoid temporary Uint8Array in BufferSource normalization
Replace TypedArrayPrototypeSlice(new Uint8Array(buf, off, len)) with new Uint8Array(ArrayBufferPrototypeSlice(buf, off, off + len)) in normalizeAlgorithm. This eliminates a throwaway view allocation per BufferSource member during algorithm normalization. Signed-off-by: Filip Skokan <panva.ip@gmail.com>
1 parent 2eb5ef6 commit 1be2d88

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/internal/crypto/util.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayBufferIsView,
55
ArrayBufferPrototypeGetByteLength,
6+
ArrayBufferPrototypeSlice,
67
ArrayPrototypeIncludes,
78
ArrayPrototypePush,
89
BigInt,
@@ -22,7 +23,6 @@ const {
2223
TypedArrayPrototypeGetBuffer,
2324
TypedArrayPrototypeGetByteLength,
2425
TypedArrayPrototypeGetByteOffset,
25-
TypedArrayPrototypeSlice,
2626
Uint8Array,
2727
} = primordials;
2828

@@ -606,12 +606,13 @@ function normalizeAlgorithm(algorithm, op) {
606606
// 3.
607607
if (idlType === 'BufferSource' && idlValue) {
608608
const isView = ArrayBufferIsView(idlValue);
609-
normalizedAlgorithm[member] = TypedArrayPrototypeSlice(
610-
new Uint8Array(
611-
isView ? getDataViewOrTypedArrayBuffer(idlValue) : idlValue,
612-
isView ? getDataViewOrTypedArrayByteOffset(idlValue) : 0,
613-
isView ? getDataViewOrTypedArrayByteLength(idlValue) : ArrayBufferPrototypeGetByteLength(idlValue),
614-
),
609+
const buffer = isView ? getDataViewOrTypedArrayBuffer(idlValue) : idlValue;
610+
const offset = isView ? getDataViewOrTypedArrayByteOffset(idlValue) : 0;
611+
const length = isView ?
612+
getDataViewOrTypedArrayByteLength(idlValue) :
613+
ArrayBufferPrototypeGetByteLength(idlValue);
614+
normalizedAlgorithm[member] = new Uint8Array(
615+
ArrayBufferPrototypeSlice(buffer, offset, offset + length),
615616
);
616617
} else if (idlType === 'HashAlgorithmIdentifier') {
617618
normalizedAlgorithm[member] = normalizeAlgorithm(idlValue, 'digest');

0 commit comments

Comments
 (0)