-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
url: make WHATWG URL properties spec compliant #10408
Closed
joyeecheung
wants to merge
2
commits into
nodejs:master
from
joyeecheung:whatwg-url-property-descriptors
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
|
||
var common = require('../common.js'); | ||
var URL = require('url').URL; | ||
|
||
var bench = common.createBenchmark(main, { | ||
url: [ | ||
'http://example.com/', | ||
'https://encrypted.google.com/search?q=url&q=site:npmjs.org&hl=en', | ||
'javascript:alert("node is awesome");', | ||
'http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test' | ||
], | ||
prop: ['toString', 'href', 'origin', 'protocol', | ||
'username', 'password', 'host', 'hostname', 'port', | ||
'pathname', 'search', 'searchParams', 'hash'], | ||
n: [1e4] | ||
}); | ||
|
||
function setAndGet(n, url, prop, alternative) { | ||
const old = url[prop]; | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
url[prop] = n % 2 === 0 ? alternative : old; // set | ||
url[prop]; // get | ||
} | ||
bench.end(n); | ||
} | ||
|
||
function get(n, url, prop) { | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
url[prop]; // get | ||
} | ||
bench.end(n); | ||
} | ||
|
||
function stringify(n, url, prop) { | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
url.toString(); | ||
} | ||
bench.end(n); | ||
} | ||
|
||
const alternatives = { | ||
href: 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test', | ||
protocol: 'https:', | ||
username: 'user2', | ||
password: 'pass2', | ||
host: 'foo.bar.net:22', | ||
hostname: 'foo.bar.org', | ||
port: '23', | ||
pathname: '/aaa/bbb', | ||
search: '?k=99', | ||
hash: '#abcd' | ||
}; | ||
|
||
function getAlternative(prop) { | ||
return alternatives[prop]; | ||
} | ||
|
||
function main(conf) { | ||
const n = conf.n | 0; | ||
const url = new URL(conf.url); | ||
const prop = conf.prop; | ||
|
||
switch (prop) { | ||
case 'protocol': | ||
case 'username': | ||
case 'password': | ||
case 'host': | ||
case 'hostname': | ||
case 'port': | ||
case 'pathname': | ||
case 'search': | ||
case 'hash': | ||
setAndGet(n, url, prop, getAlternative(prop)); | ||
break; | ||
// TODO: move href to the first group when the setter lands. | ||
case 'href': | ||
case 'origin': | ||
case 'searchParams': | ||
get(n, url, prop); | ||
break; | ||
case 'toString': | ||
stringify(n, url); | ||
break; | ||
default: | ||
throw new Error('Unknown prop'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can be
const
. The main reason for keepingvar
in other benchmarks is for compatibility with older versions of Node.js, but since the WHATWG stuff wasn't introduced until v7, it's perfectly fine to use the newer conventions here.