@@ -87,42 +87,45 @@ to one of these new APIs.*
87
87
containing a * copy* of the provided string.
88
88
* [ ` Buffer.alloc(size[, fill[, encoding]]) ` ] [ buffer_alloc ] returns a "filled"
89
89
` Buffer ` instance of the specified size. This method can be significantly
90
- slower than [ ` Buffer.allocUnsafe(size) ` ] [ buffer_allocunsafe ] but ensures that
91
- newly created ` Buffer ` instances never contain old and potentially sensitive
92
- data.
93
- * [ ` Buffer.allocUnsafe(size) ` ] [ buffer_allocunsafe ] returns a new ` Buffer ` of
94
- the specified ` size ` whose content * must* be initialized using either
95
- [ ` buf.fill(0) ` ] [ ] or written to completely.
90
+ slower than [ ` Buffer.allocUnsafe(size) ` ] [ buffer_allocunsafe ] but ensures
91
+ that newly created ` Buffer ` instances never contain old and potentially
92
+ sensitive data.
93
+ * [ ` Buffer.allocUnsafe(size) ` ] [ buffer_allocunsafe ] and
94
+ [ ` Buffer.allocUnsafeSlow(size) ` ] [ buffer_allocunsafeslow ] each return a
95
+ new ` Buffer ` of the specified ` size ` whose content * must* be initialized
96
+ using either [ ` buf.fill(0) ` ] [ ] or written to completely.
96
97
97
98
` Buffer ` instances returned by ` Buffer.allocUnsafe(size) ` * may* be allocated
98
- off a shared internal memory pool if the ` size ` is less than or equal to half
99
- ` Buffer.poolSize ` .
99
+ off a shared internal memory pool if ` size ` is less than or equal to half
100
+ ` Buffer.poolSize ` . Instances returned by ` Buffer.allocUnsafeSlow(size) ` * never*
101
+ use the shared internal memory pool.
100
102
101
103
### The ` --zero-fill-buffers ` command line option
102
104
103
105
Node.js can be started using the ` --zero-fill-buffers ` command line option to
104
106
force all newly allocated ` Buffer ` and ` SlowBuffer ` instances created using
105
- either ` new Buffer(size) ` , ` Buffer.allocUnsafe(size) ` , or
106
- ` new SlowBuffer(size) ` to be * automatically zero-filled* upon creation. Use of
107
- this flag * changes the default behavior* of these methods and * can have a
108
- significant impact* on performance. Use of the ` --zero-fill-buffers ` option is
109
- recommended only when absolutely necessary to enforce that newly allocated
110
- ` Buffer ` instances cannot contain potentially sensitive data.
107
+ either ` new Buffer(size) ` , ` Buffer.allocUnsafe(size) ` ,
108
+ ` Buffer.allocUnsafeSlow(size) ` or ` new SlowBuffer(size) ` to be * automatically
109
+ zero-filled* upon creation. Use of this flag * changes the default behavior* of
110
+ these methods and * can have a significant impact* on performance. Use of the
111
+ ` --zero-fill-buffers ` option is recommended only when absolutely necessary to
112
+ enforce that newly allocated ` Buffer ` instances cannot contain potentially
113
+ sensitive data.
111
114
112
115
```
113
116
$ node --zero-fill-buffers
114
117
> Buffer.allocUnsafe(5);
115
118
<Buffer 00 00 00 00 00>
116
119
```
117
120
118
- ### What makes ` Buffer.allocUnsafe(size) ` "unsafe"?
121
+ ### What makes ` Buffer.allocUnsafe(size) ` and ` Buffer.allocUnsafeSlow(size) ` "unsafe"?
119
122
120
- When calling ` Buffer.allocUnsafe() ` , the segment of allocated memory is
121
- * uninitialized* (it is not zeroed-out). While this design makes the allocation
122
- of memory quite fast, the allocated segment of memory might contain old data
123
- that is potentially sensitive. Using a ` Buffer ` created by
124
- ` Buffer.allocUnsafe(size ) ` without * completely* overwriting the memory can
125
- allow this old data to be leaked when the ` Buffer ` memory is read.
123
+ When calling ` Buffer.allocUnsafe() ` (and ` Buffer.allocUnsafeSlow() ` ), the
124
+ segment of allocated memory is * uninitialized* (it is not zeroed-out). While
125
+ this design makes the allocation of memory quite fast, the allocated segment of
126
+ memory might contain old data that is potentially sensitive. Using a ` Buffer `
127
+ created by ` Buffer.allocUnsafe() ` without * completely* overwriting the memory
128
+ can allow this old data to be leaked when the ` Buffer ` memory is read.
126
129
127
130
While there are clear performance advantages to using ` Buffer.allocUnsafe() ` ,
128
131
extra care * must* be taken in order to avoid introducing security
@@ -466,6 +469,52 @@ Buffer pool if `size` is less than or equal to half `Buffer.poolSize`. The
466
469
difference is subtle but can be important when an application requires the
467
470
additional performance that ` Buffer.allocUnsafe(size) ` provides.
468
471
472
+ ### Class Method: Buffer.allocUnsafeSlow(size)
473
+
474
+ * ` size ` {Number}
475
+
476
+ Allocates a new * non-zero-filled* and non-pooled ` Buffer ` of ` size ` bytes. The
477
+ ` size ` must be less than or equal to the value of
478
+ ` require('buffer').kMaxLength ` (on 64-bit architectures, ` kMaxLength ` is
479
+ ` (2^31)-1 ` ). Otherwise, a [ ` RangeError ` ] [ ] is thrown. If a ` size ` less than 0
480
+ is specified, a zero-length ` Buffer ` will be created.
481
+
482
+ The underlying memory for ` Buffer ` instances created in this way is * not
483
+ initialized* . The contents of the newly created ` Buffer ` are unknown and
484
+ * may contain sensitive data* . Use [ ` buf.fill(0) ` ] [ ] to initialize such
485
+ ` Buffer ` instances to zeroes.
486
+
487
+ When using ` Buffer.allocUnsafe() ` to allocate new ` Buffer ` instances,
488
+ allocations under 4KB are, by default, sliced from a single pre-allocated
489
+ ` Buffer ` . This allows applications to avoid the garbage collection overhead of
490
+ creating many individually allocated Buffers. This approach improves both
491
+ performance and memory usage by eliminating the need to track and cleanup as
492
+ many ` Persistent ` objects.
493
+
494
+ However, in the case where a developer may need to retain a small chunk of
495
+ memory from a pool for an indeterminate amount of time, it may be appropriate
496
+ to create an un-pooled Buffer instance using ` Buffer.allocUnsafeSlow() ` then
497
+ copy out the relevant bits.
498
+
499
+ ``` js
500
+ // need to keep around a few small chunks of memory
501
+ const store = [];
502
+
503
+ socket .on (' readable' , () => {
504
+ const data = socket .read ();
505
+ // allocate for retained data
506
+ const sb = Buffer .allocUnsafeSlow (10 );
507
+ // copy the data into the new allocation
508
+ data .copy (sb, 0 , 0 , 10 );
509
+ store .push (sb);
510
+ });
511
+ ```
512
+
513
+ Use of ` Buffer.allocUnsafeSlow() ` should be used only as a last resort * after*
514
+ a developer has observed undue memory retention in their applications.
515
+
516
+ A ` TypeError ` will be thrown if ` size ` is not a number.
517
+
469
518
### Class Method: Buffer.byteLength(string[ , encoding] )
470
519
471
520
* ` string ` {String | Buffer | TypedArray | DataView | ArrayBuffer}
@@ -1805,7 +1854,8 @@ console.log(buf);
1805
1854
[ buffer_from_buffer ] : #buffer_class_method_buffer_from_buffer
1806
1855
[ buffer_from_arraybuf ] : #buffer_class_method_buffer_from_arraybuffer
1807
1856
[ buffer_from_string ] : #buffer_class_method_buffer_from_str_encoding
1808
- [ buffer_allocunsafe ] : #buffer_class_method_buffer_allocraw_size
1857
+ [ buffer_allocunsafe ] : #buffer_class_method_buffer_allocunsafe_size
1858
+ [ buffer_allocunsafeslow ] : #buffer_class_method_buffer_allocunsafeslow_size
1809
1859
[ buffer_alloc ] : #buffer_class_method_buffer_alloc_size_fill_encoding
1810
1860
[ `TypedArray.from()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
1811
1861
[ `DataView` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
0 commit comments