Skip to content

Commit 03b20a7

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
benchmark: add benchmark for buf.compare()
There is a benchmark for the class method `Buffer.compare()` but not for the instance method `buf.compare()`. This adds that benchmark. I used this to confirm a performance regression in an implementation I was considering. While the implementation was a bust, it does seem like the benchmark is worthwhile. The benchmark is nearly identical to the existing `Buffer.compare()` benchmark except, of course, that it calls `buf.compare()` instead. PR-URL: #5441 Reviewed-By: Brian White <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent d681bf2 commit 03b20a7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const v8 = require('v8');
4+
5+
const bench = common.createBenchmark(main, {
6+
size: [16, 512, 1024, 4096, 16386],
7+
millions: [1]
8+
});
9+
10+
function main(conf) {
11+
const iter = (conf.millions >>> 0) * 1e6;
12+
const size = (conf.size >>> 0);
13+
const b0 = new Buffer(size).fill('a');
14+
const b1 = new Buffer(size).fill('a');
15+
16+
b1[size - 1] = 'b'.charCodeAt(0);
17+
18+
// Force optimization before starting the benchmark
19+
b0.compare(b1);
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
eval('%OptimizeFunctionOnNextCall(b0.compare)');
22+
b0.compare(b1);
23+
24+
bench.start();
25+
for (var i = 0; i < iter; i++) {
26+
b0.compare(b1);
27+
}
28+
bench.end(iter / 1e6);
29+
}

0 commit comments

Comments
 (0)