1'use strict'; 2 3const common = require('../common'); 4 5const bench = common.createBenchmark(main, { 6 n: [1e6], 7 operation: ['get', 'set', 'enumerate', 'query', 'delete'] 8}); 9 10 11function main({ n, operation }) { 12 switch (operation) { 13 case 'get': 14 bench.start(); 15 for (let i = 0; i < n; i++) { 16 process.env.PATH; // eslint-disable-line no-unused-expressions 17 } 18 bench.end(n); 19 break; 20 case 'set': 21 bench.start(); 22 for (let i = 0; i < n; i++) { 23 process.env.DUMMY = 'hello, world'; 24 } 25 bench.end(n); 26 break; 27 case 'enumerate': 28 // First, normalize process.env so that benchmark results are comparable. 29 for (const key of Object.keys(process.env)) 30 delete process.env[key]; 31 for (let i = 0; i < 64; i++) 32 process.env[Math.random()] = Math.random(); 33 34 n /= 10; // Enumeration is comparatively heavy. 35 bench.start(); 36 for (let i = 0; i < n; i++) { 37 // Access every item in object to process values. 38 Object.keys(process.env); 39 } 40 bench.end(n); 41 break; 42 case 'query': 43 bench.start(); 44 for (let i = 0; i < n; i++) { 45 'PATH' in process.env; // eslint-disable-line no-unused-expressions 46 } 47 bench.end(n); 48 break; 49 case 'delete': 50 bench.start(); 51 for (let i = 0; i < n; i++) { 52 delete process.env.DUMMY; 53 } 54 bench.end(n); 55 break; 56 } 57} 58