Skip to content

Commit

Permalink
benchmark: improve process.env benchmarks
Browse files Browse the repository at this point in the history
Benchmark different types of operations and make results comparable
by normalizing process.env for enumeartion.

PR-URL: #29188
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax authored and BridgeAR committed Sep 25, 2019
1 parent 3a6bc90 commit b9c7c90
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
54 changes: 47 additions & 7 deletions benchmark/process/bench-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,55 @@
const common = require('../common');

const bench = common.createBenchmark(main, {
n: [1e5],
n: [1e6],
operation: ['get', 'set', 'enumerate', 'query', 'delete']
});


function main({ n }) {
bench.start();
for (var i = 0; i < n; i++) {
// Access every item in object to process values.
Object.keys(process.env);
function main({ n, operation }) {
switch (operation) {
case 'get':
bench.start();
for (let i = 0; i < n; i++) {
process.env.PATH;
}
bench.end(n);
break;
case 'set':
bench.start();
for (let i = 0; i < n; i++) {
process.env.DUMMY = 'hello, world';
}
bench.end(n);
break;
case 'enumerate':
// First, normalize process.env so that benchmark results are comparable.
for (const key of Object.keys(process.env))
delete process.env[key];
for (let i = 0; i < 64; i++)
process.env[Math.random()] = Math.random();

n /= 10; // Enumeration is comparatively heavy.
bench.start();
for (let i = 0; i < n; i++) {
// Access every item in object to process values.
Object.keys(process.env);
}
bench.end(n);
break;
case 'query':
bench.start();
for (let i = 0; i < n; i++) {
'PATH' in process.env;
}
bench.end(n);
break;
case 'delete':
bench.start();
for (let i = 0; i < n; i++) {
delete process.env.DUMMY;
}
bench.end(n);
break;
}
bench.end(n);
}
3 changes: 2 additions & 1 deletion test/benchmark/test-benchmark-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ const runBenchmark = require('../common/benchmark');
runBenchmark('process',
[
'n=1',
'type=raw'
'type=raw',
'operation=enumerate',
], { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 comments on commit b9c7c90

Please sign in to comment.