Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 138d004

Browse filesBrowse files
JacksonTianMyles Borins
authored andcommitted
buffer: faster case for create Buffer from new Buffer(0)
When create Buffer from a Buffer will copy data from old to new even though length is zero. This patch can improve edge case 4x faster. following is benchmark results. new: buffers/buffer_zero.js n=1024: 2463.53891 old: buffers/buffer_zero.js n=1024: 618.70801 PR-URL: #4326 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3ff2373 commit 138d004
Copy full SHA for 138d004

File tree

Expand file treeCollapse file tree

2 files changed

+22
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+22
-0
lines changed
Open diff view settings
Collapse file

‎benchmark/buffers/buffer_zero.js‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1024]
7+
});
8+
9+
const zero = new Buffer(0);
10+
11+
function main(conf) {
12+
var n = +conf.n;
13+
bench.start();
14+
for (let i = 0; i < n * 1024; i++) {
15+
new Buffer(zero);
16+
}
17+
bench.end(n);
18+
}
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ function fromString(string, encoding) {
123123
function fromObject(obj) {
124124
if (obj instanceof Buffer) {
125125
var b = allocate(obj.length);
126+
127+
if (b.length === 0)
128+
return b;
129+
126130
obj.copy(b, 0, 0, obj.length);
127131
return b;
128132
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.