Repository: nodejs/node
File: lib/buffer.js (JavaScript)

Code before:
    }

    if (pos < length) {
      TypedArrayPrototypeFill(buffer, 0, pos, length);
    }
    return buffer;
  }

  validateOffset(length, 'length');
  const buffer = allocate(length);
  let pos = 0;
  for (let i = 0; i < list.length; i++) {
    const buf = list[i];
    if (!isUint8Array(buf)) {
      // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
      // Instead, find the proper error code for this.
      throw new ERR_INVALID_ARG_TYPE(
        `list[${i}]`, ['Buffer', 'Uint8Array'], buf);
    }
    if (pos + buf.length > length) {
      TypedArrayPrototypeSet(buffer,
                             TypedArrayPrototypeSlice(buf, 0, length - pos),
                             pos);
      pos = length;
      break;
    }
    TypedArrayPrototypeSet(buffer, buf, pos);
    pos += buf.length;
  }

  // Note: `length` is always equal to `buffer.length` at this point
  if (pos < length) {
    // Zero-fill the remaining bytes if the specified `length` was more than
    // the actual total length, i.e. if we have some remaining allocated bytes
    // there were not initialized.
    TypedArrayPrototypeFill(buffer, 0, pos, length);
  }

  return buffer;
};

function base64ByteLength(str, bytes) {
  // Handle padding
  if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
    bytes--;
  if (bytes > 1 && StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
    bytes--;

  // Base64 ratio: 3/4
  return (bytes * 3) >>> 2;
}

Review comment (performance):
Regarding: https://github.com/nodejs/node/pull/61721#discussion_r2777929478

It was here, sorry.

We throw `ERR_INVALID_ARG_TYPE` right now if you do `Buffer.concat([Buffer.from('a'), 'x'], 1)`.

Because we always went through `_copyActual`, and the type check was there for all arguments, even when the length was exhausted and `_copyActual` was noop

After this change, that example would pass as we stop validating arguments when no byte remains to copy

In fact some of the performance improvements might be coming from this early `break`

Just FYI

Code after:
      TypedArrayPrototypeSet(buffer, buf, pos);
      pos += bufLength;
    }

    if (pos < length) {
      TypedArrayPrototypeFill(buffer, 0, pos, length);
    }
    return buffer;
  }

  validateOffset(length, 'length');
  for (let i = 0; i < list.length; i++) {
    if (!isUint8Array(list[i])) {
      // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
      // Instead, find the proper error code for this.
      throw new ERR_INVALID_ARG_TYPE(
        `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
    }
  }

  const buffer = allocate(length);
  let pos = 0;
  for (let i = 0; i < list.length; i++) {
    const buf = list[i];
    const bufLength = TypedArrayPrototypeGetByteLength(buf);
    if (pos + bufLength > length) {
      TypedArrayPrototypeSet(buffer,
                             TypedArrayPrototypeSubarray(buf, 0, length - pos),
                             pos);
      pos = length;
      break;
    }
    TypedArrayPrototypeSet(buffer, buf, pos);
    pos += bufLength;
  }

  // Note: `length` is always equal to `buffer.length` at this point
  if (pos < length) {
    // Zero-fill the remaining bytes if the specified `length` was more than
    // the actual total length, i.e. if we have some remaining allocated bytes
    // there were not initialized.
    TypedArrayPrototypeFill(buffer, 0, pos, length);
  }

  return buffer;
};

function base64ByteLength(str, bytes) {
  // Handle padding
  if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
    bytes--;