forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy-vs-whatwg-url-parse.js
53 lines (47 loc) · 1.1 KB
/
legacy-vs-whatwg-url-parse.js
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
'use strict';
const common = require('../common.js');
const url = require('url');
const URL = url.URL;
const assert = require('assert');
const inputs = require('../fixtures/url-inputs.js').urls;
const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
method: ['legacy', 'whatwg'],
n: [1e5]
});
function useLegacy(n, input) {
var noDead = url.parse(input);
bench.start();
for (var i = 0; i < n; i += 1) {
noDead = url.parse(input);
}
bench.end(n);
return noDead;
}
function useWHATWG(n, input) {
var noDead = new URL(input);
bench.start();
for (var i = 0; i < n; i += 1) {
noDead = new URL(input);
}
bench.end(n);
return noDead;
}
function main({ type, n, method }) {
const input = inputs[type];
if (!input) {
throw new Error(`Unknown input type "${type}"`);
}
var noDead; // Avoid dead code elimination.
switch (method) {
case 'legacy':
noDead = useLegacy(n, input);
break;
case 'whatwg':
noDead = useWHATWG(n, input);
break;
default:
throw new Error(`Unknown method ${method}`);
}
assert.ok(noDead);
}