diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-06-26 06:24:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-26 06:24:48 -0700 |
commit | 6da87450edab54f82599aacba2e696460f361212 (patch) | |
tree | 2204507956b528edcd7bcdc6e66bf39f22bc656d /ext/node/polyfills/internal/buffer.mjs | |
parent | eb283c43f5601df54b7086578daef9793a36a56f (diff) |
perf: improve Buffer.from(buf) by 29x (#24341)
Diffstat (limited to 'ext/node/polyfills/internal/buffer.mjs')
-rw-r--r-- | ext/node/polyfills/internal/buffer.mjs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/ext/node/polyfills/internal/buffer.mjs b/ext/node/polyfills/internal/buffer.mjs index a051965a3..de3568a83 100644 --- a/ext/node/polyfills/internal/buffer.mjs +++ b/ext/node/polyfills/internal/buffer.mjs @@ -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); } |