Skip to content

Commit

Permalink
url: adding WHATWG URL support
Browse files Browse the repository at this point in the history
Implements WHATWG URL support. Example:

```
var u = new url.URL('http://example.org');
```

Many, many other commits improving the implementation have been squashed
into this backport PR. They are not listed separately here for brevity.

Backport-PR-URL: #17365
PR-URL: #7448
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
  • Loading branch information
jasnell authored and MylesBorins committed Feb 13, 2018
1 parent 60b10f0 commit 24def19
Show file tree
Hide file tree
Showing 57 changed files with 16,345 additions and 75 deletions.
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

0 comments on commit 24def19

Please sign in to comment.