Skip to content

Commit 2cdc20a

Browse files
committed
resolved feedback
1 parent 2a67802 commit 2cdc20a

1 file changed

Lines changed: 30 additions & 47 deletions

File tree

lib/buffer.js

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -378,46 +378,35 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
378378
}
379379

380380
const viewLength = TypedArrayPrototypeGetLength(view);
381-
if (viewLength === 0) {
382-
return new FastBuffer();
383-
}
381+
if (viewLength === 0) return new FastBuffer();
384382

385-
const byteLength = TypedArrayPrototypeGetByteLength(view);
383+
let start = 0;
384+
let end = viewLength;
386385

387-
if (offset !== undefined || length !== undefined) {
388-
if (offset !== undefined) {
389-
validateInteger(offset, 'offset', 0);
390-
if (offset >= viewLength) return new FastBuffer();
391-
} else {
392-
offset = 0;
393-
}
394-
395-
let end;
396-
if (length !== undefined) {
397-
validateInteger(length, 'length', 0);
398-
// The old code used TypedArrayPrototypeSlice which clamps internally.
399-
end = MathMin(offset + length, viewLength);
400-
} else {
401-
end = viewLength;
402-
}
386+
if (offset !== undefined) {
387+
validateInteger(offset, 'offset', 0);
388+
if (offset >= viewLength) return new FastBuffer();
389+
start = offset;
390+
}
403391

404-
if (end <= offset) return new FastBuffer();
392+
if (length !== undefined) {
393+
validateInteger(length, 'length', 0);
394+
// The old code used TypedArrayPrototypeSlice which clamps internally.
395+
end = MathMin(start + length, viewLength);
396+
}
405397

406-
const elementSize = byteLength / viewLength;
407-
const srcByteOffset = TypedArrayPrototypeGetByteOffset(view) +
408-
offset * elementSize;
409-
const srcByteLength = (end - offset) * elementSize;
398+
if (end <= start) return new FastBuffer();
410399

411-
return fromArrayLike(new Uint8Array(
412-
TypedArrayPrototypeGetBuffer(view),
413-
srcByteOffset,
414-
srcByteLength));
415-
}
400+
const byteLength = TypedArrayPrototypeGetByteLength(view);
401+
const elementSize = byteLength / viewLength;
402+
const srcByteOffset = TypedArrayPrototypeGetByteOffset(view) +
403+
start * elementSize;
404+
const srcByteLength = (end - start) * elementSize;
416405

417406
return fromArrayLike(new Uint8Array(
418407
TypedArrayPrototypeGetBuffer(view),
419-
TypedArrayPrototypeGetByteOffset(view),
420-
byteLength));
408+
srcByteOffset,
409+
srcByteLength));
421410
};
422411

423412
// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
@@ -564,15 +553,15 @@ function fromArrayBuffer(obj, byteOffset, length) {
564553
}
565554

566555
function fromArrayLike(obj) {
567-
const len = obj.length;
568-
if (len <= 0)
556+
const { length } = obj;
557+
if (length <= 0)
569558
return new FastBuffer();
570-
if (len < (Buffer.poolSize >>> 1)) {
571-
if (len > (poolSize - poolOffset))
559+
if (length < (Buffer.poolSize >>> 1)) {
560+
if (length > (poolSize - poolOffset))
572561
createPool();
573-
const b = new FastBuffer(allocPool, poolOffset, len);
562+
const b = new FastBuffer(allocPool, poolOffset, length);
574563
TypedArrayPrototypeSet(b, obj, 0);
575-
poolOffset += len;
564+
poolOffset += length;
576565
alignPool();
577566
return b;
578567
}
@@ -1270,9 +1259,7 @@ Buffer.prototype.swap16 = function swap16() {
12701259
// For Buffer.length <= 32, it's generally faster to
12711260
// do the swap in javascript. For larger buffers,
12721261
// dropping down to the native code is faster.
1273-
if (!isUint8Array(this))
1274-
throw new ERR_INVALID_ARG_TYPE('this', ['Buffer', 'Uint8Array'], this);
1275-
const len = this.length;
1262+
const len = TypedArrayPrototypeGetLength(this);
12761263
if (len % 2 !== 0)
12771264
throw new ERR_INVALID_BUFFER_SIZE('16-bits');
12781265
if (len <= 32) {
@@ -1288,9 +1275,7 @@ Buffer.prototype.swap32 = function swap32() {
12881275
// For Buffer.length <= 32, it's generally faster to
12891276
// do the swap in javascript. For larger buffers,
12901277
// dropping down to the native code is faster.
1291-
if (!isUint8Array(this))
1292-
throw new ERR_INVALID_ARG_TYPE('this', ['Buffer', 'Uint8Array'], this);
1293-
const len = this.length;
1278+
const len = TypedArrayPrototypeGetLength(this);
12941279
if (len % 4 !== 0)
12951280
throw new ERR_INVALID_BUFFER_SIZE('32-bits');
12961281
if (len <= 32) {
@@ -1308,9 +1293,7 @@ Buffer.prototype.swap64 = function swap64() {
13081293
// For Buffer.length < 48, it's generally faster to
13091294
// do the swap in javascript. For larger buffers,
13101295
// dropping down to the native code is faster.
1311-
if (!isUint8Array(this))
1312-
throw new ERR_INVALID_ARG_TYPE('this', ['Buffer', 'Uint8Array'], this);
1313-
const len = this.length;
1296+
const len = TypedArrayPrototypeGetLength(this);
13141297
if (len % 8 !== 0)
13151298
throw new ERR_INVALID_BUFFER_SIZE('64-bits');
13161299
if (len < 48) {

0 commit comments

Comments
 (0)