Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
buffer: avoid 'in' operator
This change results in ~50% improvement when creating a Buffer from
an array-like object.
  • Loading branch information
mscdex committed Dec 25, 2016
commit 801493cc98d35179a8113a15a9bddecd41c7c701
11 changes: 10 additions & 1 deletion benchmark/buffers/buffer-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const bench = common.createBenchmark(main, {
'buffer',
'uint8array',
'string',
'string-base64'
'string-base64',
'object'
],
len: [10, 2048],
n: [1024]
Expand All @@ -25,6 +26,7 @@ function main(conf) {
const str = 'a'.repeat(len);
const buffer = Buffer.allocUnsafe(len);
const uint8array = new Uint8Array(len);
const obj = { length: null }; // Results in a new, empty Buffer

var i;

Expand Down Expand Up @@ -80,6 +82,13 @@ function main(conf) {
}
bench.end(n);
break;
case 'object':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(obj);
}
bench.end(n);
break;
default:
assert.fail(null, null, 'Should not get here');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function fromObject(obj) {
}

if (obj) {
if ('length' in obj || isArrayBuffer(obj.buffer) ||
if (obj.length !== undefined || isArrayBuffer(obj.buffer) ||
isSharedArrayBuffer(obj.buffer)) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return new FastBuffer();
Expand Down