From 58f5e9b9e4448b619acb560bdfa83e01826b19a4 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 30 Aug 2016 17:16:09 +0200 Subject: [PATCH 1/4] benchmark: fix off-by-one error in fs benchmarks Fix a off-by-one error that made the benchmarks for asynchronous functions run `n - 1` times instead of `n` times. --- benchmark/fs/bench-readdir.js | 2 +- benchmark/fs/bench-realpath.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/fs/bench-readdir.js b/benchmark/fs/bench-readdir.js index 565b92d14954ca..eb15e72724d8cb 100644 --- a/benchmark/fs/bench-readdir.js +++ b/benchmark/fs/bench-readdir.js @@ -14,7 +14,7 @@ function main(conf) { bench.start(); (function r(cntr) { - if (--cntr <= 0) + if (cntr-- <= 0) return bench.end(n); fs.readdir(path.resolve(__dirname, '../../lib/'), function() { r(cntr); diff --git a/benchmark/fs/bench-realpath.js b/benchmark/fs/bench-realpath.js index 1a181935f14ec2..384276eafff9b3 100644 --- a/benchmark/fs/bench-realpath.js +++ b/benchmark/fs/bench-realpath.js @@ -27,7 +27,7 @@ function main(conf) { function relativePath(n) { (function r(cntr) { - if (--cntr <= 0) + if (cntr-- <= 0) return bench.end(n); fs.realpath(relative_path, function() { r(cntr); @@ -37,7 +37,7 @@ function relativePath(n) { function resolvedPath(n) { (function r(cntr) { - if (--cntr <= 0) + if (cntr-- <= 0) return bench.end(n); fs.realpath(resolved_path, function() { r(cntr); From 82de54182603a3af0bb9b3b83ead6810cc042233 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 30 Aug 2016 14:52:41 +0200 Subject: [PATCH 2/4] benchmark: add benches for fs.stat & fs.statSync Add very simple benchmarks for `fs.stat` and `fs.statSync` as well as `fs.lstat` and `fs.lstatSync` based on the `readdir` benchmarks. --- benchmark/fs/bench-stat.js | 24 ++++++++++++++++++++++++ benchmark/fs/bench-statSync.js | 21 +++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 benchmark/fs/bench-stat.js create mode 100644 benchmark/fs/bench-statSync.js diff --git a/benchmark/fs/bench-stat.js b/benchmark/fs/bench-stat.js new file mode 100644 index 00000000000000..daa18f224f0c19 --- /dev/null +++ b/benchmark/fs/bench-stat.js @@ -0,0 +1,24 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); + +const bench = common.createBenchmark(main, { + n: [1e4], + kind: ['lstat', 'stat'] +}); + + +function main(conf) { + const n = conf.n >>> 0; + const fn = fs[conf.kind]; + + bench.start(); + (function r(cntr) { + if (cntr-- <= 0) + return bench.end(n); + fn(__filename, function() { + r(cntr); + }); + }(n)); +} diff --git a/benchmark/fs/bench-statSync.js b/benchmark/fs/bench-statSync.js new file mode 100644 index 00000000000000..ba1e8168b4aaf5 --- /dev/null +++ b/benchmark/fs/bench-statSync.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); + +const bench = common.createBenchmark(main, { + n: [1e4], + kind: ['lstatSync', 'statSync'] +}); + + +function main(conf) { + const n = conf.n >>> 0; + const fn = fs[conf.kind]; + + bench.start(); + for (var i = 0; i < n; i++) { + fn(__filename); + } + bench.end(n); +} From 78608a067bcf4a00af75953b13c8aca67c1748da Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 9 Sep 2016 20:30:16 +0200 Subject: [PATCH 3/4] Pass used function directly to the closure --- benchmark/fs/bench-stat.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/fs/bench-stat.js b/benchmark/fs/bench-stat.js index daa18f224f0c19..3e97adb8574893 100644 --- a/benchmark/fs/bench-stat.js +++ b/benchmark/fs/bench-stat.js @@ -14,11 +14,11 @@ function main(conf) { const fn = fs[conf.kind]; bench.start(); - (function r(cntr) { + (function r(cntr, fn) { if (cntr-- <= 0) return bench.end(n); fn(__filename, function() { - r(cntr); + r(cntr, fn); }); - }(n)); + }(n, fn)); } From 514a8a7c541e6da1ac2ec4d61c1373c497873375 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 9 Sep 2016 20:37:22 +0200 Subject: [PATCH 4/4] Maybe? simplify function call/remove variable shadowing --- benchmark/fs/bench-stat.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/benchmark/fs/bench-stat.js b/benchmark/fs/bench-stat.js index 3e97adb8574893..c0db00e27deee6 100644 --- a/benchmark/fs/bench-stat.js +++ b/benchmark/fs/bench-stat.js @@ -11,7 +11,6 @@ const bench = common.createBenchmark(main, { function main(conf) { const n = conf.n >>> 0; - const fn = fs[conf.kind]; bench.start(); (function r(cntr, fn) { @@ -20,5 +19,5 @@ function main(conf) { fn(__filename, function() { r(cntr, fn); }); - }(n, fn)); + }(n, fs[conf.kind])); }