From cd0fcf20c3bcef392f88106a2a3ced62c512c007 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 13 Apr 2023 17:37:17 -0400 Subject: benchmark: differentiate whatwg and legacy url PR-URL: https://github.com/nodejs/node/pull/47377 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Matthew Aitken --- benchmark/url/legacy-url-get-prop.js | 41 ++++++++++++ benchmark/url/legacy-url-parse.js | 22 +++++++ benchmark/url/legacy-vs-whatwg-url-get-prop.js | 90 -------------------------- benchmark/url/legacy-vs-whatwg-url-parse.js | 66 ------------------- benchmark/url/whatwg-url-canParse.js | 14 ++++ benchmark/url/whatwg-url-get-prop.js | 40 ++++++++++++ benchmark/url/whatwg-url-parse.js | 42 ++++++++++++ benchmark/url/whatwg-url-to-and-from-path.js | 30 +++++++++ benchmark/url/whatwgurl-canParse.js | 14 ---- benchmark/url/whatwgurl-to-and-from-path.js | 30 --------- 10 files changed, 189 insertions(+), 200 deletions(-) create mode 100644 benchmark/url/legacy-url-get-prop.js create mode 100644 benchmark/url/legacy-url-parse.js delete mode 100644 benchmark/url/legacy-vs-whatwg-url-get-prop.js delete mode 100644 benchmark/url/legacy-vs-whatwg-url-parse.js create mode 100644 benchmark/url/whatwg-url-canParse.js create mode 100644 benchmark/url/whatwg-url-get-prop.js create mode 100644 benchmark/url/whatwg-url-parse.js create mode 100644 benchmark/url/whatwg-url-to-and-from-path.js delete mode 100644 benchmark/url/whatwgurl-canParse.js delete mode 100644 benchmark/url/whatwgurl-to-and-from-path.js (limited to 'benchmark') diff --git a/benchmark/url/legacy-url-get-prop.js b/benchmark/url/legacy-url-get-prop.js new file mode 100644 index 0000000000..09be863e96 --- /dev/null +++ b/benchmark/url/legacy-url-get-prop.js @@ -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); +} diff --git a/benchmark/url/legacy-url-parse.js b/benchmark/url/legacy-url-parse.js new file mode 100644 index 0000000000..ac893ebf8a --- /dev/null +++ b/benchmark/url/legacy-url-parse.js @@ -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); +} diff --git a/benchmark/url/legacy-vs-whatwg-url-get-prop.js b/benchmark/url/legacy-vs-whatwg-url-get-prop.js deleted file mode 100644 index df888f13b9..0000000000 --- a/benchmark/url/legacy-vs-whatwg-url-get-prop.js +++ /dev/null @@ -1,90 +0,0 @@ -'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, - method: ['legacy', 'whatwg'], - e: [1], -}); - -function useLegacy(data) { - 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); - return noDead; -} - -function useWHATWG(data) { - 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); - return noDead; -} - -function main({ type, method, e }) { - let data; - let noDead; // Avoid dead code elimination. - switch (method) { - case 'legacy': - data = common.bakeUrlData(type, e, false, false); - noDead = useLegacy(data.map((i) => url.parse(i))); - break; - case 'whatwg': - data = common.bakeUrlData(type, e, false, true); - noDead = useWHATWG(data); - break; - default: - throw new Error(`Unknown method "${method}"`); - } - - assert.ok(noDead); -} diff --git a/benchmark/url/legacy-vs-whatwg-url-parse.js b/benchmark/url/legacy-vs-whatwg-url-parse.js deleted file mode 100644 index a54d81e15f..0000000000 --- a/benchmark/url/legacy-vs-whatwg-url-parse.js +++ /dev/null @@ -1,66 +0,0 @@ -'use strict'; -const common = require('../common.js'); -const url = require('url'); -const URL = url.URL; -const assert = require('assert'); - -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 - bench.start(); - for (let i = 0; i < len; ++i) { - const item = data[i]; - result = new URL(item[0], item[1]); - } - bench.end(len); - return result; -} - -function useWHATWGWithoutBase(data) { - const len = data.length; - let result = new URL(data[0]); // Avoid dead code elimination - bench.start(); - for (let i = 0; i < len; ++i) { - result = new URL(data[i]); - } - bench.end(len); - return result; -} - -function main({ e, method, 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}`); - } - - assert.ok(noDead); -} diff --git a/benchmark/url/whatwg-url-canParse.js b/benchmark/url/whatwg-url-canParse.js new file mode 100644 index 0000000000..3896d78578 --- /dev/null +++ b/benchmark/url/whatwg-url-canParse.js @@ -0,0 +1,14 @@ +'use strict'; +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + type: Object.keys(common.urls), + n: [1e6], +}); + +function main({ type, n }) { + bench.start(); + for (let i = 0; i < n; i += 1) + URL.canParse(common.urls[type]); + bench.end(n); +} diff --git a/benchmark/url/whatwg-url-get-prop.js b/benchmark/url/whatwg-url-get-prop.js new file mode 100644 index 0000000000..3a88dd1da6 --- /dev/null +++ b/benchmark/url/whatwg-url-get-prop.js @@ -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); +} diff --git a/benchmark/url/whatwg-url-parse.js b/benchmark/url/whatwg-url-parse.js new file mode 100644 index 0000000000..0c10b12587 --- /dev/null +++ b/benchmark/url/whatwg-url-parse.js @@ -0,0 +1,42 @@ +'use strict'; +const common = require('../common.js'); +const url = require('url'); +const URL = url.URL; +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + withBase: ['true', 'false'], + type: common.urlDataTypes, + e: [1], +}); + +function useWHATWGWithBase(data) { + const len = data.length; + let result = new URL(data[0][0], data[0][1]); // Avoid dead code elimination + bench.start(); + for (let i = 0; i < len; ++i) { + const item = data[i]; + result = new URL(item[0], item[1]); + } + bench.end(len); + return result; +} + +function useWHATWGWithoutBase(data) { + const len = data.length; + let result = new URL(data[0]); // Avoid dead code elimination + bench.start(); + for (let i = 0; i < len; ++i) { + result = new URL(data[i]); + } + bench.end(len); + return result; +} + +function main({ e, type, withBase }) { + withBase = withBase === 'true'; + const data = common.bakeUrlData(type, e, withBase, false); + const noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data); + + assert.ok(noDead); +} diff --git a/benchmark/url/whatwg-url-to-and-from-path.js b/benchmark/url/whatwg-url-to-and-from-path.js new file mode 100644 index 0000000000..3b87c0670a --- /dev/null +++ b/benchmark/url/whatwg-url-to-and-from-path.js @@ -0,0 +1,30 @@ +'use strict'; +const common = require('../common.js'); +const { fileURLToPath, pathToFileURL } = require('node:url'); +const isWindows = process.platform === 'win32'; + +const bench = common.createBenchmark(main, { + input: isWindows ? [ + 'file:///c/', + ] : [ + 'file:///dev/null', + 'file:///dev/null?key=param&bool', + 'file:///dev/null?key=param&bool#hash', + ], + method: isWindows ? [ + 'fileURLToPath', + ] : [ + 'fileURLToPath', + 'pathToFileURL', + ], + n: [5e6], +}); + +function main({ n, input, method }) { + method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL; + bench.start(); + for (let i = 0; i < n; i++) { + method(input); + } + bench.end(n); +} diff --git a/benchmark/url/whatwgurl-canParse.js b/benchmark/url/whatwgurl-canParse.js deleted file mode 100644 index 3896d78578..0000000000 --- a/benchmark/url/whatwgurl-canParse.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; -const common = require('../common.js'); - -const bench = common.createBenchmark(main, { - type: Object.keys(common.urls), - n: [1e6], -}); - -function main({ type, n }) { - bench.start(); - for (let i = 0; i < n; i += 1) - URL.canParse(common.urls[type]); - bench.end(n); -} diff --git a/benchmark/url/whatwgurl-to-and-from-path.js b/benchmark/url/whatwgurl-to-and-from-path.js deleted file mode 100644 index 3b87c0670a..0000000000 --- a/benchmark/url/whatwgurl-to-and-from-path.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; -const common = require('../common.js'); -const { fileURLToPath, pathToFileURL } = require('node:url'); -const isWindows = process.platform === 'win32'; - -const bench = common.createBenchmark(main, { - input: isWindows ? [ - 'file:///c/', - ] : [ - 'file:///dev/null', - 'file:///dev/null?key=param&bool', - 'file:///dev/null?key=param&bool#hash', - ], - method: isWindows ? [ - 'fileURLToPath', - ] : [ - 'fileURLToPath', - 'pathToFileURL', - ], - n: [5e6], -}); - -function main({ n, input, method }) { - method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL; - bench.start(); - for (let i = 0; i < n; i++) { - method(input); - } - bench.end(n); -} -- cgit v1.2.1