Skip to content

Commit

Permalink
benchmark: use let/const instead of var in buffers
Browse files Browse the repository at this point in the history
PR-URL: #30945
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
dnlup authored and MylesBorins committed Dec 17, 2019
1 parent 954793f commit b6ddbc1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 31 deletions.
4 changes: 2 additions & 2 deletions benchmark/buffers/buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const chars = [
];

function main({ n, len, encoding }) {
var strings = [];
var results = [ len * 16 ];
let strings = [];
let results = [ len * 16 ];
if (encoding === 'buffer') {
strings = [ Buffer.alloc(len * 16, 'a') ];
} else {
Expand Down
11 changes: 5 additions & 6 deletions benchmark/buffers/buffer-compare-instance-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,46 @@ function main({ n, size, args }) {
const b1 = Buffer.alloc(size, 'a');
const b0Len = b0.length;
const b1Len = b1.length;
var i;

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

switch (args) {
case 2:
b0.compare(b1, 0);
bench.start();
for (i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
b0.compare(b1, 0);
}
bench.end(n);
break;
case 3:
b0.compare(b1, 0, b1Len);
bench.start();
for (i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
b0.compare(b1, 0, b1Len);
}
bench.end(n);
break;
case 4:
b0.compare(b1, 0, b1Len, 0);
bench.start();
for (i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
b0.compare(b1, 0, b1Len, 0);
}
bench.end(n);
break;
case 5:
b0.compare(b1, 0, b1Len, 0, b0Len);
bench.start();
for (i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
b0.compare(b1, 0, b1Len, 0, b0Len);
}
bench.end(n);
break;
default:
b0.compare(b1);
bench.start();
for (i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
b0.compare(b1);
}
bench.end(n);
Expand Down
5 changes: 2 additions & 3 deletions benchmark/buffers/buffer-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ const bench = common.createBenchmark(main, {

function main({ len, n }) {
const buf = Buffer.alloc(len);
var i;

for (i = 0; i < buf.length; i++)
for (let i = 0; i < buf.length; i++)
buf[i] = i & 0xff;

const hex = buf.toString('hex');

bench.start();

for (i = 0; i < n; i += 1)
for (let i = 0; i < n; i += 1)
Buffer.from(hex, 'hex');

bench.end(n);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/buffers/buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const bench = common.createBenchmark(main, {
});

function main({ n, search, encoding, type }) {
var aliceBuffer = fs.readFileSync(
let aliceBuffer = fs.readFileSync(
path.resolve(__dirname, '../fixtures/alice.html')
);

Expand Down
8 changes: 4 additions & 4 deletions benchmark/buffers/buffer-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ function main({ size, type, method, n }) {
}

function benchFor(buffer, n) {
for (var k = 0; k < n; k++) {
for (var i = 0; i < buffer.length; i++) {
for (let k = 0; k < n; k++) {
for (let i = 0; i < buffer.length; i++) {
assert(buffer[i] === 0);
}
}
}

function benchForOf(buffer, n) {
for (var k = 0; k < n; k++) {
for (let k = 0; k < n; k++) {
for (const b of buffer) {
assert(b === 0);
}
}
}

function benchIterator(buffer, n) {
for (var k = 0; k < n; k++) {
for (let k = 0; k < n; k++) {
const iter = buffer[Symbol.iterator]();
let cur = iter.next();

Expand Down
4 changes: 2 additions & 2 deletions benchmark/buffers/buffer-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Buffer.prototype.htons = function htons() {
Buffer.prototype.htonl = function htonl() {
if (this.length % 4 !== 0)
throw new RangeError();
for (var i = 0; i < this.length; i += 4) {
for (let i = 0; i < this.length; i += 4) {
swap(this, i, i + 3);
swap(this, i + 1, i + 2);
}
Expand Down Expand Up @@ -66,7 +66,7 @@ function createBuffer(len, aligned) {
function genMethod(method) {
const fnString = `
return function ${method}(n, buf) {
for (var i = 0; i <= n; i++)
for (let i = 0; i <= n; i++)
buf.${method}();
}`;
return (new Function(fnString))();
Expand Down
9 changes: 4 additions & 5 deletions benchmark/buffers/buffer-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,28 @@ function main({ encoding, args, len, n }) {
if (encoding.length === 0)
encoding = undefined;

var i;
switch (args) {
case 1:
bench.start();
for (i = 0; i < n; i += 1)
for (let i = 0; i < n; i += 1)
buf.toString(encoding);
bench.end(n);
break;
case 2:
bench.start();
for (i = 0; i < n; i += 1)
for (let i = 0; i < n; i += 1)
buf.toString(encoding, 0);
bench.end(n);
break;
case 3:
bench.start();
for (i = 0; i < n; i += 1)
for (let i = 0; i < n; i += 1)
buf.toString(encoding, 0, len);
bench.end(n);
break;
default:
bench.start();
for (i = 0; i < n; i += 1)
for (let i = 0; i < n; i += 1)
buf.toString();
bench.end(n);
break;
Expand Down
14 changes: 6 additions & 8 deletions benchmark/buffers/buffer-write-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ function main({ len, n, encoding, args }) {
let start = 0;
const buf = Buffer.allocUnsafe(len);

var i;

switch (args) {
case 'offset':
string = 'a'.repeat(Math.floor(len / 2));
start = len - string.length;
if (encoding) {
bench.start();
for (i = 0; i < n; ++i) {
for (let i = 0; i < n; ++i) {
buf.write(string, start, encoding);
}
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; ++i) {
for (let i = 0; i < n; ++i) {
buf.write(string, start);
}
bench.end(n);
Expand All @@ -39,13 +37,13 @@ function main({ len, n, encoding, args }) {
string = 'a'.repeat(len);
if (encoding) {
bench.start();
for (i = 0; i < n; ++i) {
for (let i = 0; i < n; ++i) {
buf.write(string, 0, buf.length, encoding);
}
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; ++i) {
for (let i = 0; i < n; ++i) {
buf.write(string, 0, buf.length);
}
bench.end(n);
Expand All @@ -55,13 +53,13 @@ function main({ len, n, encoding, args }) {
string = 'a'.repeat(len);
if (encoding) {
bench.start();
for (i = 0; i < n; ++i) {
for (let i = 0; i < n; ++i) {
buf.write(string, encoding);
}
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; ++i) {
for (let i = 0; i < n; ++i) {
buf.write(string);
}
bench.end(n);
Expand Down

0 comments on commit b6ddbc1

Please sign in to comment.