mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
68ba9aa0fb
In most cases, named functions match the variable or property to which they are being assigned. That also seems to be the practice in a series of PRs currently being evaluated that name currently-anonymous functions. This change applies that rule to instances in the code base that don't comply with that practice. This will be enforceable with a lint rule once we upgrade to ESLint 3.8.0. PR-URL: https://github.com/nodejs/node/pull/9113 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
91 lines
2.3 KiB
JavaScript
91 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
const v8 = require('v8');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
aligned: ['true', 'false'],
|
|
method: ['swap16', 'swap32', 'swap64'/*, 'htons', 'htonl', 'htonll'*/],
|
|
len: [8, 64, 128, 256, 512, 768, 1024, 1536, 2056, 4096, 8192],
|
|
n: [5e7]
|
|
});
|
|
|
|
// The htons and htonl methods below are used to benchmark the
|
|
// performance difference between doing the byteswap in pure
|
|
// javascript regardless of Buffer size as opposed to dropping
|
|
// down to the native layer for larger Buffer sizes. Commented
|
|
// out by default because they are slow for big buffers. If
|
|
// re-evaluating the crossover point, uncomment those methods
|
|
// and comment out their implementations in lib/buffer.js so
|
|
// C++ version will always be used.
|
|
|
|
function swap(b, n, m) {
|
|
const i = b[n];
|
|
b[n] = b[m];
|
|
b[m] = i;
|
|
}
|
|
|
|
Buffer.prototype.htons = function htons() {
|
|
if (this.length % 2 !== 0)
|
|
throw new RangeError();
|
|
for (var i = 0; i < this.length; i += 2) {
|
|
swap(this, i, i + 1);
|
|
}
|
|
return this;
|
|
};
|
|
|
|
Buffer.prototype.htonl = function htonl() {
|
|
if (this.length % 4 !== 0)
|
|
throw new RangeError();
|
|
for (var i = 0; i < this.length; i += 4) {
|
|
swap(this, i, i + 3);
|
|
swap(this, i + 1, i + 2);
|
|
}
|
|
return this;
|
|
};
|
|
|
|
Buffer.prototype.htonll = function htonll() {
|
|
if (this.length % 8 !== 0)
|
|
throw new RangeError();
|
|
for (var i = 0; i < this.length; i += 8) {
|
|
swap(this, i, i + 7);
|
|
swap(this, i + 1, i + 6);
|
|
swap(this, i + 2, i + 5);
|
|
swap(this, i + 3, i + 4);
|
|
}
|
|
return this;
|
|
};
|
|
|
|
function createBuffer(len, aligned) {
|
|
len += aligned ? 0 : 1;
|
|
const buf = Buffer.allocUnsafe(len);
|
|
for (var i = 1; i <= len; i++)
|
|
buf[i - 1] = i;
|
|
return aligned ? buf : buf.slice(1);
|
|
}
|
|
|
|
function genMethod(method) {
|
|
const fnString =
|
|
'return function ' + method + '(n, buf) {' +
|
|
' for (var i = 0; i <= n; i++)' +
|
|
' buf.' + method + '();' +
|
|
'}';
|
|
return (new Function(fnString))();
|
|
}
|
|
|
|
function main(conf) {
|
|
const method = conf.method;
|
|
const len = conf.len | 0;
|
|
const n = conf.n | 0;
|
|
const aligned = conf.aligned || 'true';
|
|
const buf = createBuffer(len, aligned === 'true');
|
|
const bufferSwap = genMethod(method);
|
|
|
|
v8.setFlagsFromString('--allow_natives_syntax');
|
|
eval('%OptimizeFunctionOnNextCall(bufferSwap)');
|
|
|
|
bench.start();
|
|
bufferSwap(n, buf);
|
|
bench.end(n);
|
|
}
|