Skip to content

Commit f864509

Browse files
ChALkeRaddaleax
authored andcommitted
test,benchmark: use new Buffer API where appropriate
For tests / benchmarks that are creating Buffer instances for any reason other than to test Buffer constructor, use the new Buffer.alloc/Buffer.from API instead of the deprecated API. PR-URL: #18980 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent cab6c8e commit f864509

12 files changed

+19
-18
lines changed

benchmark/streams/pipe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function main({ n }) {
11-
const b = new Buffer(1024);
11+
const b = Buffer.alloc(1024);
1212
const r = new Readable();
1313
const w = new Writable();
1414

benchmark/streams/readable-bigread.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function main({ n }) {
11-
const b = new Buffer(32);
11+
const b = Buffer.alloc(32);
1212
const s = new Readable();
1313
function noop() {}
1414
s._read = noop;

benchmark/streams/readable-bigunevenread.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function main({ n }) {
11-
const b = new Buffer(32);
11+
const b = Buffer.alloc(32);
1212
const s = new Readable();
1313
function noop() {}
1414
s._read = noop;

benchmark/streams/readable-readall.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function main({ n }) {
11-
const b = new Buffer(32);
11+
const b = Buffer.alloc(32);
1212
const s = new Readable();
1313
function noop() {}
1414
s._read = noop;

benchmark/streams/readable-unevenread.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function main({ n }) {
11-
const b = new Buffer(32);
11+
const b = Buffer.alloc(32);
1212
const s = new Readable();
1313
function noop() {}
1414
s._read = noop;

test/async-hooks/test-udpsendwrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const sock = dgram
1818

1919
function onlistening() {
2020
sock.send(
21-
new Buffer(2), 0, 2, sock.address().port,
21+
Buffer.alloc(2), 0, 2, sock.address().port,
2222
undefined, common.mustCall(onsent));
2323

2424
// init not called synchronously because dns lookup always wraps

test/parallel/test-buffer-badhex.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const assert = require('assert');
66
{
77
const buf = Buffer.alloc(4);
88
assert.strictEqual(buf.length, 4);
9-
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
9+
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
1010
assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2);
11-
assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0x00, 0x00]));
11+
assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0x00, 0x00]));
1212
assert.strictEqual(buf.toString('hex'), 'abcd0000');
1313
assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4);
14-
assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01]));
14+
assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0xef, 0x01]));
1515
assert.strictEqual(buf.toString('hex'), 'abcdef01');
1616

1717
const copy = Buffer.from(buf.toString('hex'), 'hex');
@@ -26,13 +26,13 @@ const assert = require('assert');
2626

2727
{
2828
const buf = Buffer.alloc(4);
29-
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
29+
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
3030
assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0);
31-
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
31+
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
3232
assert.strictEqual(buf.write('xxab', 1, 'hex'), 0);
33-
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
33+
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
3434
assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1);
35-
assert.deepStrictEqual(buf, new Buffer([0xcd, 0, 0, 0]));
35+
assert.deepStrictEqual(buf, Buffer.from([0xcd, 0, 0, 0]));
3636
}
3737

3838
{

test/parallel/test-buffer-fill.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ common.expectsError(() => {
427427

428428
// Test that bypassing 'length' won't cause an abort.
429429
common.expectsError(() => {
430-
const buf = new Buffer('w00t');
430+
const buf = Buffer.from('w00t');
431431
Object.defineProperty(buf, 'length', {
432432
value: 1337,
433433
enumerable: true

test/parallel/test-buffer-indexof.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1);
504504
assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1);
505505

506506
// Test lastIndexOf on a longer buffer:
507-
const bufferString = new Buffer('a man a plan a canal panama');
507+
const bufferString = Buffer.from('a man a plan a canal panama');
508508
assert.strictEqual(15, bufferString.lastIndexOf('canal'));
509509
assert.strictEqual(21, bufferString.lastIndexOf('panama'));
510510
assert.strictEqual(0, bufferString.lastIndexOf('a man a plan a canal panama'));
@@ -566,7 +566,7 @@ const parts = [];
566566
for (let i = 0; i < 1000000; i++) {
567567
parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag');
568568
}
569-
const reallyLong = new Buffer(parts.join(' '));
569+
const reallyLong = Buffer.from(parts.join(' '));
570570
assert.strictEqual('yolo swag swag yolo', reallyLong.slice(0, 19).toString());
571571

572572
// Expensive reverse searches. Stress test lastIndexOf:

test/parallel/test-buffer-zero-fill.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require('../common');
44
const assert = require('assert');
55

6+
// Tests deprecated Buffer API on purpose
67
const buf1 = Buffer(100);
78
const buf2 = new Buffer(100);
89

test/parallel/test-net-socket-byteswritten.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ server.listen(0, common.mustCall(function() {
1616
socket.cork();
1717

1818
socket.write('one');
19-
socket.write(new Buffer('twø', 'utf8'));
19+
socket.write(Buffer.from('twø', 'utf8'));
2020

2121
socket.uncork();
2222

test/parallel/test-zlib-empty-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
const zlib = require('zlib');
44
const { inspect, promisify } = require('util');
55
const assert = require('assert');
6-
const emptyBuffer = new Buffer(0);
6+
const emptyBuffer = Buffer.alloc(0);
77

88
common.crashOnUnhandledRejection();
99

0 commit comments

Comments
 (0)