Skip to content

Commit

Permalink
perf: improve Buffer.from(buf) by 29x (#24341)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Jun 26, 2024
1 parent eb283c4 commit 6da8745
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ext/node/polyfills/internal/buffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 229,22 @@ function fromArrayLike(array) {
return buf;
}

function fromUint8Array(u8) {
const buf = new Uint8Array(u8.buffer, u8.byteOffset, u8.byteLength);
Object.setPrototypeOf(buf, Buffer.prototype);
return buf.slice();
}

function fromObject(obj) {
if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
if (typeof obj.length !== "number") {
return createBuffer(0);
}

if (obj instanceof Uint8Array) {
return fromUint8Array(obj);
}

return fromArrayLike(obj);
}

Expand Down

0 comments on commit 6da8745

Please sign in to comment.