Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2016-12-22 21:58:45 +0300
committerEvan Lucas <evanlucas@me.com>2017-01-04 19:44:09 +0300
commit0b2bc5e27b746d816f98154ae85936e544371043 (patch)
treee786bae82b9cb573dd7c4f218201f1e9246d5332 /benchmark
parent785975d922f2e9a27b5615537485f9cab23dbccf (diff)
benchmark: add benchmark for WHATWG URL properties
PR-URL: https://github.com/nodejs/node/pull/10408 Fixes: https://github.com/nodejs/node/issues/10376 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r--benchmark/url/whatwg-url-properties.js91
1 files changed, 91 insertions, 0 deletions
diff --git a/benchmark/url/whatwg-url-properties.js b/benchmark/url/whatwg-url-properties.js
new file mode 100644
index 00000000000..a3c4d886bd3
--- /dev/null
+++ b/benchmark/url/whatwg-url-properties.js
@@ -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');
+ }
+}