1'use strict'; 2const common = require('../common.js'); 3const url = require('url'); 4const URL = url.URL; 5const assert = require('assert'); 6 7const bench = common.createBenchmark(main, { 8 type: common.urlDataTypes, 9 method: ['legacy', 'whatwg'], 10 e: [1] 11}); 12 13function useLegacy(data) { 14 const obj = url.parse(data[0]); 15 const len = data.length; 16 let noDead = url.format(obj); 17 bench.start(); 18 for (let i = 0; i < len; i++) { 19 noDead = data[i].toString(); 20 } 21 bench.end(len); 22 return noDead; 23} 24 25function useWHATWG(data) { 26 const obj = new URL(data[0]); 27 const len = data.length; 28 let noDead = obj.toString(); 29 bench.start(); 30 for (let i = 0; i < len; i++) { 31 noDead = data[i].toString(); 32 } 33 bench.end(len); 34 return noDead; 35} 36 37function main({ type, e, method }) { 38 const data = common.bakeUrlData(type, e, false, false); 39 40 let noDead; // Avoid dead code elimination. 41 switch (method) { 42 case 'legacy': 43 noDead = useLegacy(data); 44 break; 45 case 'whatwg': 46 noDead = useWHATWG(data); 47 break; 48 default: 49 throw new Error(`Unknown method ${method}`); 50 } 51 52 assert.ok(noDead); 53} 54