• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 noDead = {
16    protocol: obj.protocol,
17    auth: obj.auth,
18    host: obj.host,
19    hostname: obj.hostname,
20    port: obj.port,
21    pathname: obj.pathname,
22    search: obj.search,
23    hash: obj.hash
24  };
25  const len = data.length;
26  // It's necessary to assign the values to an object
27  // to avoid loop invariant code motion.
28  bench.start();
29  for (let i = 0; i < len; i++) {
30    const obj = data[i];
31    noDead.protocol = obj.protocol;
32    noDead.auth = obj.auth;
33    noDead.host = obj.host;
34    noDead.hostname = obj.hostname;
35    noDead.port = obj.port;
36    noDead.pathname = obj.pathname;
37    noDead.search = obj.search;
38    noDead.hash = obj.hash;
39  }
40  bench.end(len);
41  return noDead;
42}
43
44function useWHATWG(data) {
45  const obj = new URL(data[0]);
46  const noDead = {
47    protocol: obj.protocol,
48    auth: `${obj.username}:${obj.password}`,
49    host: obj.host,
50    hostname: obj.hostname,
51    port: obj.port,
52    pathname: obj.pathname,
53    search: obj.search,
54    hash: obj.hash
55  };
56  const len = data.length;
57  bench.start();
58  for (let i = 0; i < len; i++) {
59    const obj = data[i];
60    noDead.protocol = obj.protocol;
61    noDead.auth = `${obj.username}:${obj.password}`;
62    noDead.host = obj.host;
63    noDead.hostname = obj.hostname;
64    noDead.port = obj.port;
65    noDead.pathname = obj.pathname;
66    noDead.search = obj.search;
67    noDead.hash = obj.hash;
68  }
69  bench.end(len);
70  return noDead;
71}
72
73function main({ type, method, e }) {
74  let data;
75  let noDead;  // Avoid dead code elimination.
76  switch (method) {
77    case 'legacy':
78      data = common.bakeUrlData(type, e, false, false);
79      noDead = useLegacy(data.map((i) => url.parse(i)));
80      break;
81    case 'whatwg':
82      data = common.bakeUrlData(type, e, false, true);
83      noDead = useWHATWG(data);
84      break;
85    default:
86      throw new Error(`Unknown method "${method}"`);
87  }
88
89  assert.ok(noDead);
90}
91