Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: differentiate whatwg and legacy url #47377

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions benchmark/url/legacy-url-get-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common.js');
const url = require('url');
const assert = require('assert');

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

function main({ type, e }) {
const data = common.bakeUrlData(type, e, false, false).map((i) => url.parse(i));
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 (let 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);
assert.ok(noDead);
}
22 changes: 22 additions & 0 deletions benchmark/url/legacy-url-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common.js');
const url = require('url');
const assert = require('assert');

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

function main({ e, type }) {
const data = common.bakeUrlData(type, e, false, false);
let result = url.parse(data[0]); // Avoid dead code elimination

bench.start();
for (let i = 0; i < data.length; ++i) {
result = url.parse(data[i]);
}
bench.end(data.length);

assert.ok(result);
}
90 changes: 0 additions & 90 deletions benchmark/url/legacy-vs-whatwg-url-get-prop.js

This file was deleted.

40 changes: 40 additions & 0 deletions benchmark/url/whatwg-url-get-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'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,
e: [1],
});

function main({ type, e }) {
const data = common.bakeUrlData(type, e, false, true);
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 (let 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);
assert.ok(noDead);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@ const bench = common.createBenchmark(main, {
withBase: ['true', 'false'],
type: common.urlDataTypes,
e: [1],
method: ['legacy', 'whatwg'],
});

function useLegacy(data) {
const len = data.length;
let result = url.parse(data[0]); // Avoid dead code elimination
bench.start();
for (let i = 0; i < len; ++i) {
result = url.parse(data[i]);
}
bench.end(len);
return result;
}

function useWHATWGWithBase(data) {
const len = data.length;
let result = new URL(data[0][0], data[0][1]); // Avoid dead code elimination
Expand All @@ -45,22 +33,10 @@ function useWHATWGWithoutBase(data) {
return result;
}

function main({ e, method, type, withBase }) {
function main({ e, type, withBase }) {
withBase = withBase === 'true';
let noDead; // Avoid dead code elimination.
let data;
switch (method) {
case 'legacy':
data = common.bakeUrlData(type, e, false, false);
noDead = useLegacy(data);
break;
case 'whatwg':
data = common.bakeUrlData(type, e, withBase, false);
noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data);
break;
default:
throw new Error(`Unknown method ${method}`);
}
const data = common.bakeUrlData(type, e, withBase, false);
const noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data);

assert.ok(noDead);
}