Skip to content

Commit

Permalink
buffer: faster integer argument check
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jul 28, 2024
1 parent 8a41d9b commit 1cdad69
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
MathFloor,
MathMin,
MathTrunc,
NumberIsInteger,
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
Expand Down Expand Up @@ -212,30 +213,27 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) {
if (targetStart === undefined) {
targetStart = 0;
} else {
targetStart = toInteger(targetStart, 0);
targetStart = NumberIsInteger(targetStart) ? targetStart : toInteger(targetStart, 0);
if (targetStart < 0)
throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
}

if (sourceStart === undefined) {
sourceStart = 0;
} else {
sourceStart = toInteger(sourceStart, 0);
sourceStart = NumberIsInteger(targetStart) ? targetStart : toInteger(sourceStart, 0);
if (sourceStart < 0 || sourceStart > source.length)
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.length}`, sourceStart);
}

if (sourceEnd === undefined) {
sourceEnd = source.length;
} else {
sourceEnd = toInteger(sourceEnd, 0);
sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0);
if (sourceEnd < 0)
throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
}

if (targetStart >= target.length || sourceStart >= sourceEnd)
return 0;

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

Expand All @@ -248,6 +246,9 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
if (nb > sourceLen)
nb = sourceLen;

if (nb === 0)
return 0;

if (sourceStart !== 0 || sourceEnd < source.length)
source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb);

Expand Down

0 comments on commit 1cdad69

Please sign in to comment.