2016-03-24 05:27:39 +08:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
method: ['offset', 'slice'],
|
|
|
|
size: [16, 512, 1024, 4096, 16386],
|
|
|
|
millions: [1]
|
|
|
|
});
|
|
|
|
|
|
|
|
function compareUsingSlice(b0, b1, len, iter) {
|
2018-01-23 20:17:39 +08:00
|
|
|
for (var i = 0; i < iter; i++)
|
2016-03-24 05:27:39 +08:00
|
|
|
Buffer.compare(b0.slice(1, len), b1.slice(1, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareUsingOffset(b0, b1, len, iter) {
|
2018-01-23 20:17:39 +08:00
|
|
|
for (var i = 0; i < iter; i++)
|
2016-03-24 05:27:39 +08:00
|
|
|
b0.compare(b1, 1, len, 1, len);
|
|
|
|
}
|
|
|
|
|
2017-12-30 10:59:57 +08:00
|
|
|
function main({ millions, size, method }) {
|
|
|
|
const iter = millions * 1e6;
|
|
|
|
const fn = method === 'slice' ? compareUsingSlice : compareUsingOffset;
|
2018-01-23 20:17:39 +08:00
|
|
|
bench.start();
|
2017-12-30 10:59:57 +08:00
|
|
|
fn(Buffer.alloc(size, 'a'),
|
|
|
|
Buffer.alloc(size, 'b'),
|
|
|
|
size >> 1,
|
|
|
|
iter);
|
2018-01-23 20:17:39 +08:00
|
|
|
bench.end(millions);
|
2016-03-24 05:27:39 +08:00
|
|
|
}
|