mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
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 ```
This commit is contained in:
parent
77ba0019e7
commit
86e0292733
2 changed files with 11 additions and 10 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -534,15 +534,18 @@ Deno.test({
|
|||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/buffer] Buffer from another buffer creates a Buffer",
|
||||
name: "[node/buffer] Buffer from another buffer creates a copy",
|
||||
fn() {
|
||||
const buffer: Buffer = Buffer.from(Buffer.from("test"));
|
||||
assertEquals(buffer.length, 4, "Buffer length should be 4");
|
||||
const buffer1: Buffer = Buffer.from("test");
|
||||
const buffer2: Buffer = Buffer.from(buffer1);
|
||||
assertEquals(buffer2.length, 4, "Buffer length should be 4");
|
||||
assertEquals(
|
||||
buffer.toString(),
|
||||
buffer2.toString(),
|
||||
"test",
|
||||
"Buffer to string should recover the string",
|
||||
);
|
||||
buffer1[0] = 114;
|
||||
assertEquals(buffer2.toString(), "test", "Buffer should be a copy");
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue