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 44ee33f

Browse filesBrowse files
JacksonTianMyles Borins
authored andcommitted
buffer: refactor create buffer
Use createBuffer to reduce new Uint8Array() and setPrototypeOf. PR-URL: #4340 Reviewed-By: Michaël Zasso <mic.besace@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 901172a commit 44ee33f
Copy full SHA for 44ee33f

File tree

Expand file treeCollapse file tree

1 file changed

+9
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+9
-11
lines changed
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+9-11Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ binding.setupBufferJS(Buffer.prototype, bindingObj);
1919
const flags = bindingObj.flags;
2020
const kNoZeroFill = 0;
2121

22+
function createBuffer(size) {
23+
const ui8 = new Uint8Array(size);
24+
Object.setPrototypeOf(ui8, Buffer.prototype);
25+
return ui8;
26+
}
2227

2328
function createPool() {
2429
poolSize = Buffer.poolSize;
2530
if (poolSize > 0)
2631
flags[kNoZeroFill] = 1;
27-
allocPool = new Uint8Array(poolSize);
28-
Object.setPrototypeOf(allocPool, Buffer.prototype);
32+
allocPool = createBuffer(poolSize);
2933
poolOffset = 0;
3034
}
3135
createPool();
@@ -67,9 +71,7 @@ function SlowBuffer(length) {
6771
length = 0;
6872
if (length > 0)
6973
flags[kNoZeroFill] = 1;
70-
const ui8 = new Uint8Array(+length);
71-
Object.setPrototypeOf(ui8, Buffer.prototype);
72-
return ui8;
74+
return createBuffer(+length);
7375
}
7476

7577
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -78,9 +80,7 @@ Object.setPrototypeOf(SlowBuffer, Uint8Array);
7880

7981
function allocate(size) {
8082
if (size === 0) {
81-
const ui8 = new Uint8Array(size);
82-
Object.setPrototypeOf(ui8, Buffer.prototype);
83-
return ui8;
83+
return createBuffer(size);
8484
}
8585
if (size < (Buffer.poolSize >>> 1)) {
8686
if (size > (poolSize - poolOffset))
@@ -95,9 +95,7 @@ function allocate(size) {
9595
// being zero filled.
9696
if (size > 0)
9797
flags[kNoZeroFill] = 1;
98-
const ui8 = new Uint8Array(size);
99-
Object.setPrototypeOf(ui8, Buffer.prototype);
100-
return ui8;
98+
return createBuffer(size);
10199
}
102100
}
103101

0 commit comments

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