Skip to content

Commit

Permalink
benchmark: add benchmark for buf.compare()
Browse files Browse the repository at this point in the history
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 <mscdex@mscdex.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
  • Loading branch information
Trott committed Feb 28, 2016
1 parent dcfda10 commit ffdc046
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions benchmark/buffers/buffer-compare-instance-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common.js');
const v8 = require('v8');

const bench = common.createBenchmark(main, {
size: [16, 512, 1024, 4096, 16386],
millions: [1]
});

function main(conf) {
const iter = (conf.millions >>> 0) * 1e6;
const size = (conf.size >>> 0);
const b0 = new Buffer(size).fill('a');
const b1 = new Buffer(size).fill('a');

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

// Force optimization before starting the benchmark
b0.compare(b1);
v8.setFlagsFromString('--allow_natives_syntax');
eval('%OptimizeFunctionOnNextCall(b0.compare)');
b0.compare(b1);

bench.start();
for (var i = 0; i < iter; i++) {
b0.compare(b1);
}
bench.end(iter / 1e6);
}

0 comments on commit ffdc046

Please sign in to comment.