From 86e0292733d6d08bf338b68fd50863aef17b1e44 Mon Sep 17 00:00:00 2001 From: ud2 Date: Thu, 27 Jun 2024 05:11:56 +0800 Subject: perf(ext/node): improve `Buffer.from(buffer)` (#24352) Benchmark code from #24341. ```shellsession $ deno run --allow-hrtime bench.mjs 6479.111583 $ target/release/deno run --allow-hrtime bench.mjs 962.753875 $ node bench.mjs 855.174875 ``` --- ext/node/polyfills/internal/buffer.mjs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'ext/node/polyfills') diff --git a/ext/node/polyfills/internal/buffer.mjs b/ext/node/polyfills/internal/buffer.mjs index a051965a3..c32494555 100644 --- a/ext/node/polyfills/internal/buffer.mjs +++ b/ext/node/polyfills/internal/buffer.mjs @@ -220,12 +220,9 @@ function fromString(string, encoding) { return buf; } -function fromArrayLike(array) { - const length = array.length < 0 ? 0 : checked(array.length) | 0; - const buf = createBuffer(length); - for (let i = 0; i < length; i += 1) { - buf[i] = array[i] & 255; - } +function fromArrayLike(obj) { + const buf = new Uint8Array(obj); + Object.setPrototypeOf(buf, Buffer.prototype); return buf; } @@ -234,6 +231,7 @@ function fromObject(obj) { if (typeof obj.length !== "number") { return createBuffer(0); } + return fromArrayLike(obj); } -- cgit v1.2.3