summaryrefslogtreecommitdiff
path: root/benchmark/url/legacy-vs-whatwg-url-get-prop.js
blob: 59bb4724f42ef3a6f016ba69f92704fac67ea222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
const common = require('../common.js');
const url = require('url');
const URL = url.URL;
const assert = require('assert');

const bench = common.createBenchmark(main, {
  type: common.urlDataTypes,
  method: ['legacy', 'whatwg'],
  e: [1]
});

function useLegacy(data) {
  const obj = url.parse(data[0]);
  const noDead = {
    protocol: obj.protocol,
    auth: obj.auth,
    host: obj.host,
    hostname: obj.hostname,
    port: obj.port,
    pathname: obj.pathname,
    search: obj.search,
    hash: obj.hash
  };
  const len = data.length;
  // It's necessary to assign the values to an object
  // to avoid loop invariant code motion.
  bench.start();
  for (var i = 0; i < len; i++) {
    const obj = data[i];
    noDead.protocol = obj.protocol;
    noDead.auth = obj.auth;
    noDead.host = obj.host;
    noDead.hostname = obj.hostname;
    noDead.port = obj.port;
    noDead.pathname = obj.pathname;
    noDead.search = obj.search;
    noDead.hash = obj.hash;
  }
  bench.end(len);
  return noDead;
}

function useWHATWG(data) {
  const obj = new URL(data[0]);
  const noDead = {
    protocol: obj.protocol,
    auth: `${obj.username}:${obj.password}`,
    host: obj.host,
    hostname: obj.hostname,
    port: obj.port,
    pathname: obj.pathname,
    search: obj.search,
    hash: obj.hash
  };
  const len = data.length;
  bench.start();
  for (var i = 0; i < len; i++) {
    const obj = data[i];
    noDead.protocol = obj.protocol;
    noDead.auth = `${obj.username}:${obj.password}`;
    noDead.host = obj.host;
    noDead.hostname = obj.hostname;
    noDead.port = obj.port;
    noDead.pathname = obj.pathname;
    noDead.search = obj.search;
    noDead.hash = obj.hash;
  }
  bench.end(len);
  return noDead;
}

function main({ type, method, e }) {
  e = +e;
  var data;
  var noDead;  // Avoid dead code elimination.
  switch (method) {
    case 'legacy':
      data = common.bakeUrlData(type, e, false, false);
      noDead = useLegacy(data.map((i) => url.parse(i)));
      break;
    case 'whatwg':
      data = common.bakeUrlData(type, e, false, true);
      noDead = useWHATWG(data);
      break;
    default:
      throw new Error(`Unknown method "${method}"`);
  }

  assert.ok(noDead);
}