Skip to content

Commit 0a50b68

Browse files
gurgundayChALkeR
andcommitted
alternative implementation
Co-authored-by: Nikita Skovoroda <[email protected]>
1 parent ffd22c1 commit 0a50b68

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

lib/buffer.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
252252
if (nb <= 0)
253253
return 0;
254254

255-
_copy(source, target, targetStart, sourceStart, nb);
255+
if (sourceStart === 0 && nb === sourceLen) {
256+
TypedArrayPrototypeSet(target, source, targetStart);
257+
} else {
258+
_copy(source, target, targetStart, sourceStart, nb);
259+
}
256260

257261
return nb;
258262
}
@@ -582,9 +586,8 @@ Buffer.concat = function concat(list, length) {
582586
if (length === undefined) {
583587
length = 0;
584588
for (let i = 0; i < list.length; i++) {
585-
const bufLength = list[i].length;
586-
if (bufLength) {
587-
length += bufLength;
589+
if (list[i].length) {
590+
length += list[i].length;
588591
}
589592
}
590593
} else {
@@ -601,27 +604,7 @@ Buffer.concat = function concat(list, length) {
601604
throw new ERR_INVALID_ARG_TYPE(
602605
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
603606
}
604-
605-
const bufLength = buf.length;
606-
if (bufLength === 0 || pos >= length) {
607-
// Empty buf, or destination is full and subsequent copies are no-ops.
608-
// We don't break to preserve compatibility and throw if isUint8Array fails.
609-
continue;
610-
}
611-
612-
const available = length - pos;
613-
const toCopy = bufLength > available ? available : bufLength;
614-
if (toCopy === bufLength) {
615-
TypedArrayPrototypeSet(buffer, buf, pos);
616-
} else {
617-
const src = new Uint8Array(
618-
TypedArrayPrototypeGetBuffer(buf),
619-
TypedArrayPrototypeGetByteOffset(buf),
620-
toCopy,
621-
);
622-
TypedArrayPrototypeSet(buffer, src, pos);
623-
}
624-
pos += toCopy;
607+
pos += _copyActual(buf, buffer, pos, 0, buf.length);
625608
}
626609

627610
// Note: `length` is always equal to `buffer.length` at this point

0 commit comments

Comments
 (0)