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

Backport WHATWG URL to 6.x #17365

Closed
wants to merge 4 commits into from
Closed
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
97 changes: 97 additions & 0 deletions benchmark/url/legacy-vs-whatwg-url-get-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'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]
});

// At the time of writing, when using a passed property name to index
// the object, Crankshaft would generate a LoadKeyedGeneric even when it
// remains a constant in the function, so here we must use the literal
// instead to get a LoadNamedField.
function useLegacy(n, input) {
const obj = url.parse(input);
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
};
// It's necessary to assign the values to an object
// to avoid loop invariant code motion.
bench.start();
for (var i = 0; i < n; i += 1) {
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(n);
return noDead;
}

function useWHATWG(n, input) {
const obj = new URL(input);
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
};
bench.start();
for (var i = 0; i < n; i += 1) {
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(n);
return noDead;
}

function main(conf) {
const type = conf.type;
const n = conf.n | 0;
const method = conf.method;

const input = inputs[type];
if (!input) {
throw new Error('Unknown input 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');
}

assert.ok(noDead);
}
57 changes: 57 additions & 0 deletions benchmark/url/legacy-vs-whatwg-url-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'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(conf) {
const type = conf.type;
const n = conf.n | 0;
const method = conf.method;

const input = inputs[type];
if (!input) {
throw new Error('Unknown input 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');
}

assert.ok(noDead);
}
51 changes: 51 additions & 0 deletions benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const inputs = require('../fixtures/url-inputs.js').searchParams;

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
method: ['legacy', 'whatwg'],
n: [1e6]
});

function useLegacy(n, input) {
querystring.parse(input);
bench.start();
for (var i = 0; i < n; i += 1) {
querystring.parse(input);
}
bench.end(n);
}

function useWHATWG(n, input) {
new URLSearchParams(input);
bench.start();
for (var i = 0; i < n; i += 1) {
new URLSearchParams(input);
}
bench.end(n);
}

function main(conf) {
const type = conf.type;
const n = conf.n | 0;
const method = conf.method;

const input = inputs[type];
if (!input) {
throw new Error('Unknown input type');
}

switch (method) {
case 'legacy':
useLegacy(n, input);
break;
case 'whatwg':
useWHATWG(n, input);
break;
default:
throw new Error('Unknown method');
}
}
53 changes: 53 additions & 0 deletions benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const inputs = require('../fixtures/url-inputs.js').searchParams;

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
method: ['legacy', 'whatwg'],
n: [1e6]
});

function useLegacy(n, input, prop) {
const obj = querystring.parse(input);
querystring.stringify(obj);
bench.start();
for (var i = 0; i < n; i += 1) {
querystring.stringify(obj);
}
bench.end(n);
}

function useWHATWG(n, input, prop) {
const obj = new URLSearchParams(input);
obj.toString();
bench.start();
for (var i = 0; i < n; i += 1) {
obj.toString();
}
bench.end(n);
}

function main(conf) {
const type = conf.type;
const n = conf.n | 0;
const method = conf.method;

const input = inputs[type];
if (!input) {
throw new Error('Unknown input type');
}

switch (method) {
case 'legacy':
useLegacy(n, input);
break;
case 'whatwg':
useWHATWG(n, input);
break;
default:
throw new Error('Unknown method');
}
}
59 changes: 59 additions & 0 deletions benchmark/url/legacy-vs-whatwg-url-serialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'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, prop) {
const obj = url.parse(input);
var noDead = url.format(obj);
bench.start();
for (var i = 0; i < n; i += 1) {
noDead = url.format(obj);
}
bench.end(n);
return noDead;
}

function useWHATWG(n, input, prop) {
const obj = new URL(input);
var noDead = obj.toString();
bench.start();
for (var i = 0; i < n; i += 1) {
noDead = obj.toString();
}
bench.end(n);
return noDead;
}

function main(conf) {
const type = conf.type;
const n = conf.n | 0;
const method = conf.method;

const input = inputs[type];
if (!input) {
throw new Error('Unknown input 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');
}

assert.ok(noDead);
}
61 changes: 61 additions & 0 deletions benchmark/url/url-searchparams-iteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');
const { URLSearchParams } = require('url');

const bench = common.createBenchmark(main, {
method: ['forEach', 'iterator'],
n: [1e6]
});

const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';

function forEach(n) {
const params = new URLSearchParams(str);
const noDead = [];
const cb = (val, key) => {
noDead[0] = key;
noDead[1] = val;
};

bench.start();
for (var i = 0; i < n; i += 1)
params.forEach(cb);
bench.end(n);

assert.strictEqual(noDead[0], 'three');
assert.strictEqual(noDead[1], '3rd');
}

function iterator(n) {
const params = new URLSearchParams(str);
const noDead = [];

bench.start();
for (var i = 0; i < n; i += 1) {
for (const pair of params) {
noDead[0] = pair[0];
noDead[1] = pair[1];
}
}
bench.end(n);

assert.strictEqual(noDead[0], 'three');
assert.strictEqual(noDead[1], '3rd');
}

function main(conf) {
const method = conf.method;
const n = conf.n | 0;

switch (method) {
case 'forEach':
forEach(n);
break;
case 'iterator':
iterator(n);
break;
default:
throw new Error('Unknown method');
}
}
Loading