From c77ae0def97cfbc42e0e0dd2bde97f17d5f6cb7c Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 25 Feb 2016 16:26:39 -0800 Subject: [PATCH 1/3] 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. --- .../buffers/buffer-compare-instance-method.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 benchmark/buffers/buffer-compare-instance-method.js diff --git a/benchmark/buffers/buffer-compare-instance-method.js b/benchmark/buffers/buffer-compare-instance-method.js new file mode 100644 index 00000000000000..0c94f577a965fe --- /dev/null +++ b/benchmark/buffers/buffer-compare-instance-method.js @@ -0,0 +1,22 @@ +'use strict'; +var common = require('../common.js'); + +var bench = common.createBenchmark(main, { + size: [16, 512, 1024, 4096, 16386], + millions: [1] +}); + +function main(conf) { + var iter = (conf.millions >>> 0) * 1e6; + var size = (conf.size >>> 0); + var b0 = new Buffer(size).fill('a'); + var b1 = new Buffer(size).fill('a'); + + b1[size - 1] = 'b'.charCodeAt(0); + + bench.start(); + for (var i = 0; i < iter; i++) { + b0.compare(b1); + } + bench.end(iter / 1e6); +} From 1c45f57ff909bf4a0ff3ded62d0d02a6e2120856 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 25 Feb 2016 16:46:48 -0800 Subject: [PATCH 2/3] fixup: constify, force optimization --- .../buffers/buffer-compare-instance-method.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/benchmark/buffers/buffer-compare-instance-method.js b/benchmark/buffers/buffer-compare-instance-method.js index 0c94f577a965fe..a7e341ff4839ab 100644 --- a/benchmark/buffers/buffer-compare-instance-method.js +++ b/benchmark/buffers/buffer-compare-instance-method.js @@ -1,19 +1,26 @@ 'use strict'; -var common = require('../common.js'); +const common = require('../common.js'); +const v8 = require('v8'); -var bench = common.createBenchmark(main, { +const bench = common.createBenchmark(main, { size: [16, 512, 1024, 4096, 16386], millions: [1] }); function main(conf) { - var iter = (conf.millions >>> 0) * 1e6; - var size = (conf.size >>> 0); - var b0 = new Buffer(size).fill('a'); - var b1 = new Buffer(size).fill('a'); + 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(b0, b1); + v8.setFlagsFromString('--allow_natives_syntax'); + eval('%OptimizeFunctionOnNextCall(b0.compare)'); + b0.compare(b0, b1); + bench.start(); for (var i = 0; i < iter; i++) { b0.compare(b1); From e85b2044d6e35d9d976626363bcdeb56a48c6a78 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 25 Feb 2016 16:51:54 -0800 Subject: [PATCH 3/3] fixup: parameter typo --- benchmark/buffers/buffer-compare-instance-method.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/buffers/buffer-compare-instance-method.js b/benchmark/buffers/buffer-compare-instance-method.js index a7e341ff4839ab..0becbeee23a7d7 100644 --- a/benchmark/buffers/buffer-compare-instance-method.js +++ b/benchmark/buffers/buffer-compare-instance-method.js @@ -16,10 +16,10 @@ function main(conf) { b1[size - 1] = 'b'.charCodeAt(0); // Force optimization before starting the benchmark - b0.compare(b0, b1); + b0.compare(b1); v8.setFlagsFromString('--allow_natives_syntax'); eval('%OptimizeFunctionOnNextCall(b0.compare)'); - b0.compare(b0, b1); + b0.compare(b1); bench.start(); for (var i = 0; i < iter; i++) {