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 withBase: ['true', 'false'], 9 type: common.urlDataTypes, 10 e: [1], 11 method: ['legacy', 'whatwg'] 12}); 13 14function useLegacy(data) { 15 const len = data.length; 16 let result = url.parse(data[0]); // Avoid dead code elimination 17 bench.start(); 18 for (let i = 0; i < len; ++i) { 19 result = url.parse(data[i]); 20 } 21 bench.end(len); 22 return result; 23} 24 25function useWHATWGWithBase(data) { 26 const len = data.length; 27 let result = new URL(data[0][0], data[0][1]); // Avoid dead code elimination 28 bench.start(); 29 for (let i = 0; i < len; ++i) { 30 const item = data[i]; 31 result = new URL(item[0], item[1]); 32 } 33 bench.end(len); 34 return result; 35} 36 37function useWHATWGWithoutBase(data) { 38 const len = data.length; 39 let result = new URL(data[0]); // Avoid dead code elimination 40 bench.start(); 41 for (let i = 0; i < len; ++i) { 42 result = new URL(data[i]); 43 } 44 bench.end(len); 45 return result; 46} 47 48function main({ e, method, type, withBase }) { 49 withBase = withBase === 'true'; 50 let noDead; // Avoid dead code elimination. 51 let data; 52 switch (method) { 53 case 'legacy': 54 data = common.bakeUrlData(type, e, false, false); 55 noDead = useLegacy(data); 56 break; 57 case 'whatwg': 58 data = common.bakeUrlData(type, e, withBase, false); 59 noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data); 60 break; 61 default: 62 throw new Error(`Unknown method ${method}`); 63 } 64 65 assert.ok(noDead); 66} 67