@@ -19,13 +19,17 @@ binding.setupBufferJS(Buffer.prototype, bindingObj);
19
19
const flags = bindingObj . flags ;
20
20
const kNoZeroFill = 0 ;
21
21
22
+ function createBuffer ( size ) {
23
+ const ui8 = new Uint8Array ( size ) ;
24
+ Object . setPrototypeOf ( ui8 , Buffer . prototype ) ;
25
+ return ui8 ;
26
+ }
22
27
23
28
function createPool ( ) {
24
29
poolSize = Buffer . poolSize ;
25
30
if ( poolSize > 0 )
26
31
flags [ kNoZeroFill ] = 1 ;
27
- allocPool = new Uint8Array ( poolSize ) ;
28
- Object . setPrototypeOf ( allocPool , Buffer . prototype ) ;
32
+ allocPool = createBuffer ( poolSize ) ;
29
33
poolOffset = 0 ;
30
34
}
31
35
createPool ( ) ;
@@ -67,9 +71,7 @@ function SlowBuffer(length) {
67
71
length = 0 ;
68
72
if ( length > 0 )
69
73
flags [ kNoZeroFill ] = 1 ;
70
- const ui8 = new Uint8Array ( + length ) ;
71
- Object . setPrototypeOf ( ui8 , Buffer . prototype ) ;
72
- return ui8 ;
74
+ return createBuffer ( + length ) ;
73
75
}
74
76
75
77
Object . setPrototypeOf ( SlowBuffer . prototype , Uint8Array . prototype ) ;
@@ -78,9 +80,7 @@ Object.setPrototypeOf(SlowBuffer, Uint8Array);
78
80
79
81
function allocate ( size ) {
80
82
if ( size === 0 ) {
81
- const ui8 = new Uint8Array ( size ) ;
82
- Object . setPrototypeOf ( ui8 , Buffer . prototype ) ;
83
- return ui8 ;
83
+ return createBuffer ( size ) ;
84
84
}
85
85
if ( size < ( Buffer . poolSize >>> 1 ) ) {
86
86
if ( size > ( poolSize - poolOffset ) )
@@ -95,9 +95,7 @@ function allocate(size) {
95
95
// being zero filled.
96
96
if ( size > 0 )
97
97
flags [ kNoZeroFill ] = 1 ;
98
- const ui8 = new Uint8Array ( size ) ;
99
- Object . setPrototypeOf ( ui8 , Buffer . prototype ) ;
100
- return ui8 ;
98
+ return createBuffer ( size ) ;
101
99
}
102
100
}
103
101
0 commit comments