Skip to content

Commit 85ab4a5

Browse files
committed
buffer: add .from(), .alloc() and .allocUnsafe()
Several changes: * Soft-Deprecate Buffer() constructors * Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()` * Add `--zero-fill-buffers` command line option * Add byteOffset and length to `new Buffer(arrayBuffer)` constructor * buffer.fill('') previously had no effect, now zero-fills * Update the docs PR-URL: #4682 Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 90a5fc2 commit 85ab4a5

File tree

229 files changed

+2824
-853
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+2824
-853
lines changed

benchmark/buffers/buffer-base64-decode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function main(conf) {
88
const s = 'abcd'.repeat(8 << 20);
99
s.match(/./); // Flatten string.
1010
assert.equal(s.length % 4, 0);
11-
const b = Buffer(s.length / 4 * 3);
11+
const b = Buffer.allocUnsafe(s.length / 4 * 3);
1212
b.write(s, 0, s.length, 'base64');
1313
bench.start();
1414
for (var i = 0; i < 32; i += 1) b.base64Write(s, 0, s.length);

benchmark/buffers/buffer-base64-encode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var bench = common.createBenchmark(main, {});
55

66
function main(conf) {
77
var N = 64 * 1024 * 1024;
8-
var b = Buffer(N);
8+
var b = Buffer.allocUnsafe(N);
99
var s = '';
1010
var i;
1111
for (i = 0; i < 256; ++i) s += String.fromCharCode(i);

benchmark/buffers/buffer-bytelength.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function main(conf) {
2828
strings.push(data);
2929
} else if (encoding === 'base64') {
3030
// Base64 strings will be much longer than their UTF8 counterparts
31-
strings.push(new Buffer(data, 'utf8').toString('base64'));
31+
strings.push(Buffer.from(data, 'utf8').toString('base64'));
3232
}
3333
}
3434

benchmark/buffers/buffer-compare.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ var bench = common.createBenchmark(main, {
77
});
88

99
function main(conf) {
10-
var iter = (conf.millions >>> 0) * 1e6;
11-
var size = (conf.size >>> 0);
12-
var b0 = new Buffer(size).fill('a');
13-
var b1 = new Buffer(size).fill('a');
10+
const iter = (conf.millions >>> 0) * 1e6;
11+
const size = (conf.size >>> 0);
12+
const b0 = Buffer.alloc(size, 'a');
13+
const b1 = Buffer.alloc(size, 'a');
1414

1515
b1[size - 1] = 'b'.charCodeAt(0);
1616

benchmark/buffers/buffer-creation.js

+50-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
11
'use strict';
22
const SlowBuffer = require('buffer').SlowBuffer;
33

4-
var common = require('../common.js');
5-
var bench = common.createBenchmark(main, {
6-
type: ['fast', 'slow'],
7-
len: [10, 1024],
4+
const common = require('../common.js');
5+
const assert = require('assert');
6+
const bench = common.createBenchmark(main, {
7+
type: [
8+
'fast-alloc',
9+
'fast-alloc-fill',
10+
'fast-allocUnsafe',
11+
'slow',
12+
'buffer()'],
13+
len: [10, 1024, 2048, 4096, 8192],
814
n: [1024]
915
});
1016

1117
function main(conf) {
12-
var len = +conf.len;
13-
var n = +conf.n;
14-
var clazz = conf.type === 'fast' ? Buffer : SlowBuffer;
15-
bench.start();
16-
for (var i = 0; i < n * 1024; i++) {
17-
new clazz(len);
18+
const len = +conf.len;
19+
const n = +conf.n;
20+
switch (conf.type) {
21+
case 'fast-alloc':
22+
bench.start();
23+
for (let i = 0; i < n * 1024; i++) {
24+
Buffer.alloc(len);
25+
}
26+
bench.end(n);
27+
break;
28+
case 'fast-alloc-fill':
29+
bench.start();
30+
for (let i = 0; i < n * 1024; i++) {
31+
Buffer.alloc(len, 0);
32+
}
33+
bench.end(n);
34+
break;
35+
case 'fast-allocUnsafe':
36+
bench.start();
37+
for (let i = 0; i < n * 1024; i++) {
38+
Buffer.allocUnsafe(len);
39+
}
40+
bench.end(n);
41+
break;
42+
case 'slow':
43+
bench.start();
44+
for (let i = 0; i < n * 1024; i++) {
45+
SlowBuffer(len);
46+
}
47+
bench.end(n);
48+
break;
49+
case 'buffer()':
50+
bench.start();
51+
for (let i = 0; i < n * 1024; i++) {
52+
Buffer(len);
53+
}
54+
bench.end(n);
55+
break;
56+
default:
57+
assert.fail(null, null, 'Should not get here');
1858
}
19-
bench.end(n);
2059
}

benchmark/buffers/buffer-indexof.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function main(conf) {
2727
}
2828

2929
if (encoding === 'ucs2') {
30-
aliceBuffer = new Buffer(aliceBuffer.toString(), encoding);
30+
aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding);
3131
}
3232

3333
if (conf.type === 'buffer') {
34-
search = new Buffer(new Buffer(search).toString(), encoding);
34+
search = Buffer.from(Buffer.from(search).toString(), encoding);
3535
}
3636

3737
bench.start();

benchmark/buffers/buffer-slice.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var bench = common.createBenchmark(main, {
77
n: [1024]
88
});
99

10-
var buf = new Buffer(1024);
10+
var buf = Buffer.allocUnsafe(1024);
1111
var slowBuf = new SlowBuffer(1024);
1212

1313
function main(conf) {

benchmark/buffers/buffer-tostring.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function main(conf) {
1212
const arg = conf.arg === 'true';
1313
const len = conf.len | 0;
1414
const n = conf.n | 0;
15-
const buf = Buffer(len).fill(42);
15+
const buf = Buffer.alloc(len, 42);
1616

1717
var i;
1818
bench.start();

benchmark/buffers/buffer_zero.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const bench = common.createBenchmark(main, {
66
n: [1024]
77
});
88

9-
const zero = new Buffer(0);
9+
const zero = Buffer.alloc(0);
1010

1111
function main(conf) {
1212
var n = +conf.n;
1313
bench.start();
1414
for (let i = 0; i < n * 1024; i++) {
15-
new Buffer(zero);
15+
Buffer.from(zero);
1616
}
1717
bench.end(n);
1818
}

benchmark/crypto/aes-gcm-throughput.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var bench = common.createBenchmark(main, {
99
});
1010

1111
function main(conf) {
12-
var message = (new Buffer(conf.len)).fill('b');
12+
var message = Buffer.alloc(conf.len, 'b');
1313
var key = crypto.randomBytes(keylen[conf.cipher]);
1414
var iv = crypto.randomBytes(12);
15-
var associate_data = (new Buffer(16)).fill('z');
15+
var associate_data = Buffer.alloc(16, 'z');
1616
bench.start();
1717
AEAD_Bench(conf.cipher, message, associate_data, key, iv, conf.n, conf.len);
1818
}

benchmark/crypto/cipher-stream.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ function main(conf) {
4848
encoding = 'utf8';
4949
break;
5050
case 'buf':
51-
message = new Buffer(conf.len);
52-
message.fill('b');
51+
message = Buffer.alloc(conf.len, 'b');
5352
break;
5453
default:
5554
throw new Error('unknown message type: ' + conf.type);

benchmark/crypto/hash-stream-creation.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ function main(conf) {
3333
encoding = 'utf8';
3434
break;
3535
case 'buf':
36-
message = new Buffer(conf.len);
37-
message.fill('b');
36+
message = Buffer.alloc(conf.len, 'b');
3837
break;
3938
default:
4039
throw new Error('unknown message type: ' + conf.type);
@@ -58,7 +57,7 @@ function legacyWrite(algo, message, encoding, writes, len, outEnc) {
5857

5958
// include buffer creation costs for older versions
6059
if (outEnc === 'buffer' && typeof res === 'string')
61-
res = new Buffer(res, 'binary');
60+
res = Buffer.from(res, 'binary');
6261
}
6362

6463
bench.end(gbits);

benchmark/crypto/hash-stream-throughput.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ function main(conf) {
3232
encoding = 'utf8';
3333
break;
3434
case 'buf':
35-
message = new Buffer(conf.len);
36-
message.fill('b');
35+
message = Buffer.alloc(conf.len, 'b');
3736
break;
3837
default:
3938
throw new Error('unknown message type: ' + conf.type);

benchmark/crypto/rsa-encrypt-decrypt-throughput.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ var bench = common.createBenchmark(main, {
2323
});
2424

2525
function main(conf) {
26-
var message = (new Buffer(conf.len)).fill('b');
27-
26+
var message = Buffer.alloc(conf.len, 'b');
2827
bench.start();
2928
StreamWrite(conf.algo, conf.keylen, message, conf.n, conf.len);
3029
}

benchmark/crypto/rsa-sign-verify-throughput.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ var bench = common.createBenchmark(main, {
2424
});
2525

2626
function main(conf) {
27-
var message = (new Buffer(conf.len)).fill('b');
28-
27+
var message = Buffer.alloc(conf.len, 'b');
2928
bench.start();
3029
StreamWrite(conf.algo, conf.keylen, message, conf.writes, conf.len);
3130
}

benchmark/dgram/array-vs-concat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function main(conf) {
3131

3232
chunk = [];
3333
for (var i = 0; i < chunks; i++) {
34-
chunk.push(new Buffer(Math.round(len / chunks)));
34+
chunk.push(Buffer.allocUnsafe(Math.round(len / chunks)));
3535
}
3636

3737
server();

benchmark/dgram/multi-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function main(conf) {
3131

3232
chunk = [];
3333
for (var i = 0; i < chunks; i++) {
34-
chunk.push(new Buffer(Math.round(len / chunks)));
34+
chunk.push(Buffer.allocUnsafe(Math.round(len / chunks)));
3535
}
3636

3737
server();

benchmark/dgram/offset-length.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function main(conf) {
2525
len = +conf.len;
2626
num = +conf.num;
2727
type = conf.type;
28-
chunk = new Buffer(len);
28+
chunk = Buffer.allocUnsafe(len);
2929
server();
3030
}
3131

benchmark/dgram/single-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function main(conf) {
2525
len = +conf.len;
2626
num = +conf.num;
2727
type = conf.type;
28-
chunk = new Buffer(len);
28+
chunk = Buffer.allocUnsafe(len);
2929
server();
3030
}
3131

benchmark/fs-write-stream-throughput.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ function runTest(dur, size, type) {
4545
chunk = new Array(size + 1).join('a');
4646
break;
4747
case 'buffer':
48-
chunk = new Buffer(size);
49-
chunk.fill('a');
48+
chunk = Buffer.alloc(size, 'a');
5049
break;
5150
}
5251

benchmark/fs/read-stream-throughput.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function runTest() {
6060
}
6161

6262
function makeFile() {
63-
var buf = new Buffer(filesize / 1024);
63+
var buf = Buffer.allocUnsafe(filesize / 1024);
6464
if (encoding === 'utf8') {
6565
// ü
6666
for (var i = 0; i < buf.length; i++) {

benchmark/fs/readfile.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ var bench = common.createBenchmark(main, {
1717
function main(conf) {
1818
var len = +conf.len;
1919
try { fs.unlinkSync(filename); } catch (e) {}
20-
var data = new Buffer(len);
21-
data.fill('x');
20+
var data = Buffer.alloc(len, 'x');
2221
fs.writeFileSync(filename, data);
2322
data = null;
2423

benchmark/fs/write-stream-throughput.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ function main(conf) {
2121
var chunk;
2222
switch (type) {
2323
case 'buf':
24-
chunk = new Buffer(size);
25-
chunk.fill('b');
24+
chunk = Buffer.alloc(size, 'b');
2625
break;
2726
case 'asc':
2827
chunk = new Array(size + 1).join('a');

benchmark/http/bench-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function main(conf) {
2525
}
2626
header += CRLF;
2727

28-
processHeader(new Buffer(header), n);
28+
processHeader(Buffer.from(header), n);
2929
}
3030

3131

benchmark/http/chunked.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ var bench = common.createBenchmark(main, {
1818

1919
function main(conf) {
2020
const http = require('http');
21-
var chunk = new Buffer(conf.size);
22-
chunk.fill('8');
21+
var chunk = Buffer.alloc(conf.size, '8');
2322

2423
var args = ['-d', '10s', '-t', 8, '-c', conf.c];
2524

benchmark/http/client-request-body.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ function main(conf) {
1919
var chunk;
2020
switch (conf.type) {
2121
case 'buf':
22-
chunk = new Buffer(len);
23-
chunk.fill('x');
22+
chunk = Buffer.alloc(len, 'x');
2423
break;
2524
case 'utf':
2625
encoding = 'utf8';

benchmark/http/end-vs-write-end.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ function main(conf) {
2323
var len = conf.kb * 1024;
2424
switch (conf.type) {
2525
case 'buf':
26-
chunk = new Buffer(len);
27-
chunk.fill('x');
26+
chunk = Buffer.alloc(len, 'x');
2827
break;
2928
case 'utf':
3029
chunk = new Array(len / 2 + 1).join('ü');

benchmark/http_simple.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var server = module.exports = http.createServer(function(req, res) {
5151
if (n <= 0)
5252
throw new Error('buffer called with n <= 0');
5353
if (storedBuffer[n] === undefined) {
54-
storedBuffer[n] = new Buffer(n);
54+
storedBuffer[n] = Buffer.allocUnsafe(n);
5555
for (i = 0; i < n; i++) {
5656
storedBuffer[n][i] = 'C'.charCodeAt(0);
5757
}

benchmark/http_simple_auto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var server = http.createServer(function(req, res) {
4747
n = parseInt(arg, 10);
4848
if (n <= 0) throw new Error('bytes called with n <= 0');
4949
if (storedBuffer[n] === undefined) {
50-
storedBuffer[n] = new Buffer(n);
50+
storedBuffer[n] = Buffer.allocUnsafe(n);
5151
for (i = 0; i < n; i++) {
5252
storedBuffer[n][i] = 'C'.charCodeAt(0);
5353
}

benchmark/net/net-c2s-cork.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ function main(conf) {
2323

2424
switch (type) {
2525
case 'buf':
26-
chunk = new Buffer(len);
27-
chunk.fill('x');
26+
chunk = Buffer.alloc(len, 'x');
2827
break;
2928
case 'utf':
3029
encoding = 'utf8';

benchmark/net/net-c2s.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ function main(conf) {
2323

2424
switch (type) {
2525
case 'buf':
26-
chunk = new Buffer(len);
27-
chunk.fill('x');
26+
chunk = Buffer.alloc(len, 'x');
2827
break;
2928
case 'utf':
3029
encoding = 'utf8';

benchmark/net/net-pipe.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ function main(conf) {
2323

2424
switch (type) {
2525
case 'buf':
26-
chunk = new Buffer(len);
27-
chunk.fill('x');
26+
chunk = Buffer.alloc(len, 'x');
2827
break;
2928
case 'utf':
3029
encoding = 'utf8';

0 commit comments

Comments
 (0)