mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
perf: improve Buffer.from(buf) by 29x (#24341)
This commit is contained in:
parent
eb283c43f5
commit
6da87450ed
1 changed files with 11 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue