Skip to content

Commit fe813ec

Browse files
joyeecheungevanlucas
authored andcommitted
benchmark: add benchmark for WHATWG URL properties
PR-URL: #10408 Fixes: #10376 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 96158f9 commit fe813ec

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
var common = require('../common.js');
4+
var URL = require('url').URL;
5+
6+
var bench = common.createBenchmark(main, {
7+
url: [
8+
'http://example.com/',
9+
'https://encrypted.google.com/search?q=url&q=site:npmjs.org&hl=en',
10+
'javascript:alert("node is awesome");',
11+
'http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test'
12+
],
13+
prop: ['toString', 'href', 'origin', 'protocol',
14+
'username', 'password', 'host', 'hostname', 'port',
15+
'pathname', 'search', 'searchParams', 'hash'],
16+
n: [1e4]
17+
});
18+
19+
function setAndGet(n, url, prop, alternative) {
20+
const old = url[prop];
21+
bench.start();
22+
for (var i = 0; i < n; i += 1) {
23+
url[prop] = n % 2 === 0 ? alternative : old; // set
24+
url[prop]; // get
25+
}
26+
bench.end(n);
27+
}
28+
29+
function get(n, url, prop) {
30+
bench.start();
31+
for (var i = 0; i < n; i += 1) {
32+
url[prop]; // get
33+
}
34+
bench.end(n);
35+
}
36+
37+
function stringify(n, url, prop) {
38+
bench.start();
39+
for (var i = 0; i < n; i += 1) {
40+
url.toString();
41+
}
42+
bench.end(n);
43+
}
44+
45+
const alternatives = {
46+
href: 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test',
47+
protocol: 'https:',
48+
username: 'user2',
49+
password: 'pass2',
50+
host: 'foo.bar.net:22',
51+
hostname: 'foo.bar.org',
52+
port: '23',
53+
pathname: '/aaa/bbb',
54+
search: '?k=99',
55+
hash: '#abcd'
56+
};
57+
58+
function getAlternative(prop) {
59+
return alternatives[prop];
60+
}
61+
62+
function main(conf) {
63+
const n = conf.n | 0;
64+
const url = new URL(conf.url);
65+
const prop = conf.prop;
66+
67+
switch (prop) {
68+
case 'protocol':
69+
case 'username':
70+
case 'password':
71+
case 'host':
72+
case 'hostname':
73+
case 'port':
74+
case 'pathname':
75+
case 'search':
76+
case 'hash':
77+
setAndGet(n, url, prop, getAlternative(prop));
78+
break;
79+
// TODO: move href to the first group when the setter lands.
80+
case 'href':
81+
case 'origin':
82+
case 'searchParams':
83+
get(n, url, prop);
84+
break;
85+
case 'toString':
86+
stringify(n, url);
87+
break;
88+
default:
89+
throw new Error('Unknown prop');
90+
}
91+
}

0 commit comments

Comments
 (0)