Skip to content

Commit 324f2c6

Browse files
committed
buffer: faster type check
Also add support for any TypedArray as target. PR-URL: #54088
1 parent 7941b4b commit 324f2c6

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lib/buffer.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const {
2525
Array,
26+
ArrayBufferIsView,
2627
ArrayIsArray,
2728
ArrayPrototypeForEach,
2829
MathFloor,
@@ -201,9 +202,9 @@ function toInteger(n, defaultVal) {
201202
}
202203

203204
function _copy(source, target, targetStart, sourceStart, sourceEnd) {
204-
if (!isUint8Array(source))
205+
if (!ArrayBufferIsView(source))
205206
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
206-
if (!isUint8Array(target))
207+
if (!ArrayBufferIsView(target))
207208
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
208209

209210
if (targetStart === undefined) {
@@ -218,34 +219,34 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) {
218219
sourceStart = 0;
219220
} else {
220221
sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0);
221-
if (sourceStart < 0 || sourceStart > source.length)
222-
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.length}`, sourceStart);
222+
if (sourceStart < 0 || sourceStart > source.byteLength)
223+
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart);
223224
}
224225

225226
if (sourceEnd === undefined) {
226-
sourceEnd = source.length;
227+
sourceEnd = source.byteLength;
227228
} else {
228229
sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0);
229230
if (sourceEnd < 0)
230231
throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
231232
}
232233

233-
if (targetStart >= target.length || sourceStart >= sourceEnd)
234+
if (targetStart >= target.byteLength || sourceStart >= sourceEnd)
234235
return 0;
235236

236237
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
237238
}
238239

239240
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
240-
if (sourceEnd - sourceStart > target.length - targetStart)
241-
sourceEnd = sourceStart + target.length - targetStart;
241+
if (sourceEnd - sourceStart > target.byteLength - targetStart)
242+
sourceEnd = sourceStart + target.byteLength - targetStart;
242243

243244
let nb = sourceEnd - sourceStart;
244-
const sourceLen = source.length - sourceStart;
245+
const sourceLen = source.byteLength - sourceStart;
245246
if (nb > sourceLen)
246247
nb = sourceLen;
247248

248-
if (sourceStart !== 0 || sourceEnd < source.length)
249+
if (sourceStart !== 0 || sourceEnd < source.byteLength)
249250
source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb);
250251

251252
TypedArrayPrototypeSet(target, source, targetStart);

0 commit comments

Comments
 (0)