Skip to content

Commit 40324a1

Browse files
anonrigtargos
authored andcommitted
benchmark: differentiate whatwg and legacy url
PR-URL: #47377 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
1 parent 0dc5080 commit 40324a1

7 files changed

+106
-117
lines changed

benchmark/url/legacy-url-get-prop.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const url = require('url');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
type: common.urlDataTypes,
8+
e: [1],
9+
});
10+
11+
function main({ type, e }) {
12+
const data = common.bakeUrlData(type, e, false, false).map((i) => url.parse(i));
13+
const obj = url.parse(data[0]);
14+
const noDead = {
15+
protocol: obj.protocol,
16+
auth: obj.auth,
17+
host: obj.host,
18+
hostname: obj.hostname,
19+
port: obj.port,
20+
pathname: obj.pathname,
21+
search: obj.search,
22+
hash: obj.hash,
23+
};
24+
const len = data.length;
25+
// It's necessary to assign the values to an object
26+
// to avoid loop invariant code motion.
27+
bench.start();
28+
for (let i = 0; i < len; i++) {
29+
const obj = data[i];
30+
noDead.protocol = obj.protocol;
31+
noDead.auth = obj.auth;
32+
noDead.host = obj.host;
33+
noDead.hostname = obj.hostname;
34+
noDead.port = obj.port;
35+
noDead.pathname = obj.pathname;
36+
noDead.search = obj.search;
37+
noDead.hash = obj.hash;
38+
}
39+
bench.end(len);
40+
assert.ok(noDead);
41+
}

benchmark/url/legacy-url-parse.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const url = require('url');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
type: common.urlDataTypes,
8+
e: [1],
9+
});
10+
11+
function main({ e, type }) {
12+
const data = common.bakeUrlData(type, e, false, false);
13+
let result = url.parse(data[0]); // Avoid dead code elimination
14+
15+
bench.start();
16+
for (let i = 0; i < data.length; ++i) {
17+
result = url.parse(data[i]);
18+
}
19+
bench.end(data.length);
20+
21+
assert.ok(result);
22+
}

benchmark/url/legacy-vs-whatwg-url-get-prop.js

-90
This file was deleted.
File renamed without changes.

benchmark/url/whatwg-url-get-prop.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const url = require('url');
4+
const URL = url.URL;
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
type: common.urlDataTypes,
9+
e: [1],
10+
});
11+
12+
function main({ type, e }) {
13+
const data = common.bakeUrlData(type, e, false, true);
14+
const obj = new URL(data[0]);
15+
const noDead = {
16+
protocol: obj.protocol,
17+
auth: `${obj.username}:${obj.password}`,
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+
bench.start();
27+
for (let i = 0; i < len; i++) {
28+
const obj = data[i];
29+
noDead.protocol = obj.protocol;
30+
noDead.auth = `${obj.username}:${obj.password}`;
31+
noDead.host = obj.host;
32+
noDead.hostname = obj.hostname;
33+
noDead.port = obj.port;
34+
noDead.pathname = obj.pathname;
35+
noDead.search = obj.search;
36+
noDead.hash = obj.hash;
37+
}
38+
bench.end(len);
39+
assert.ok(noDead);
40+
}

benchmark/url/legacy-vs-whatwg-url-parse.js benchmark/url/whatwg-url-parse.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,8 @@ const bench = common.createBenchmark(main, {
88
withBase: ['true', 'false'],
99
type: common.urlDataTypes,
1010
e: [1],
11-
method: ['legacy', 'whatwg'],
1211
});
1312

14-
function useLegacy(data) {
15-
const len = data.length;
16-
let result = url.parse(data[0]); // Avoid dead code elimination
17-
bench.start();
18-
for (let i = 0; i < len; ++i) {
19-
result = url.parse(data[i]);
20-
}
21-
bench.end(len);
22-
return result;
23-
}
24-
2513
function useWHATWGWithBase(data) {
2614
const len = data.length;
2715
let result = new URL(data[0][0], data[0][1]); // Avoid dead code elimination
@@ -45,22 +33,10 @@ function useWHATWGWithoutBase(data) {
4533
return result;
4634
}
4735

48-
function main({ e, method, type, withBase }) {
36+
function main({ e, type, withBase }) {
4937
withBase = withBase === 'true';
50-
let noDead; // Avoid dead code elimination.
51-
let data;
52-
switch (method) {
53-
case 'legacy':
54-
data = common.bakeUrlData(type, e, false, false);
55-
noDead = useLegacy(data);
56-
break;
57-
case 'whatwg':
58-
data = common.bakeUrlData(type, e, withBase, false);
59-
noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data);
60-
break;
61-
default:
62-
throw new Error(`Unknown method ${method}`);
63-
}
38+
const data = common.bakeUrlData(type, e, withBase, false);
39+
const noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data);
6440

6541
assert.ok(noDead);
6642
}

0 commit comments

Comments
 (0)