11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst url = require('url');
41cb0ef41Sopenharmony_ciconst URL = url.URL;
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
81cb0ef41Sopenharmony_ci  type: common.urlDataTypes,
91cb0ef41Sopenharmony_ci  method: ['legacy', 'whatwg'],
101cb0ef41Sopenharmony_ci  e: [1],
111cb0ef41Sopenharmony_ci});
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction useLegacy(data) {
141cb0ef41Sopenharmony_ci  const obj = url.parse(data[0]);
151cb0ef41Sopenharmony_ci  const len = data.length;
161cb0ef41Sopenharmony_ci  let noDead = url.format(obj);
171cb0ef41Sopenharmony_ci  bench.start();
181cb0ef41Sopenharmony_ci  for (let i = 0; i < len; i++) {
191cb0ef41Sopenharmony_ci    noDead = data[i].toString();
201cb0ef41Sopenharmony_ci  }
211cb0ef41Sopenharmony_ci  bench.end(len);
221cb0ef41Sopenharmony_ci  return noDead;
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction useWHATWG(data) {
261cb0ef41Sopenharmony_ci  const obj = new URL(data[0]);
271cb0ef41Sopenharmony_ci  const len = data.length;
281cb0ef41Sopenharmony_ci  let noDead = obj.toString();
291cb0ef41Sopenharmony_ci  bench.start();
301cb0ef41Sopenharmony_ci  for (let i = 0; i < len; i++) {
311cb0ef41Sopenharmony_ci    noDead = data[i].toString();
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci  bench.end(len);
341cb0ef41Sopenharmony_ci  return noDead;
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cifunction main({ type, e, method }) {
381cb0ef41Sopenharmony_ci  const data = common.bakeUrlData(type, e, false, false);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  let noDead;  // Avoid dead code elimination.
411cb0ef41Sopenharmony_ci  switch (method) {
421cb0ef41Sopenharmony_ci    case 'legacy':
431cb0ef41Sopenharmony_ci      noDead = useLegacy(data);
441cb0ef41Sopenharmony_ci      break;
451cb0ef41Sopenharmony_ci    case 'whatwg':
461cb0ef41Sopenharmony_ci      noDead = useWHATWG(data);
471cb0ef41Sopenharmony_ci      break;
481cb0ef41Sopenharmony_ci    default:
491cb0ef41Sopenharmony_ci      throw new Error(`Unknown method ${method}`);
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  assert.ok(noDead);
531cb0ef41Sopenharmony_ci}
54