Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: improve copy() performance #33214

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions benchmark/buffers/buffer-copy.js
Expand Up @@ -4,16 +4,18 @@ const common = require('../common.js');
const bench = common.createBenchmark(main, {
bytes: [0, 8, 128, 32 * 1024],
partial: ['true', 'false'],
oob: ['true', 'false'],
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
n: [6e6]
});

function main({ n, bytes, partial }) {
function main({ n, bytes, partial, oob }) {
const source = Buffer.allocUnsafe(bytes);
const target = Buffer.allocUnsafe(bytes);
const target = Buffer.allocUnsafe(bytes * 2);
const sourceStart = (partial === 'true' ? Math.floor(bytes / 2) : 0);
const sourceEnd = (oob === 'true' ? source.length + 1 : source.length);
bench.start();
for (let i = 0; i < n; i++) {
source.copy(target, 0, sourceStart);
source.copy(target, 0, sourceStart, sourceEnd);
}
bench.end(n);
}
2 changes: 1 addition & 1 deletion lib/buffer.js
Expand Up @@ -257,7 +257,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
if (nb > sourceLen)
nb = sourceLen;

if (sourceStart !== 0 || sourceEnd !== source.length)
if (sourceStart !== 0 || sourceEnd < source.length)
source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb);

target.set(source, targetStart);
Expand Down