mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
e571fd4f6c
PR-URL: https://github.com/nodejs/node/pull/11264 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
60 lines
1.2 KiB
JavaScript
60 lines
1.2 KiB
JavaScript
'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) {
|
|
var 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) {
|
|
var 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);
|
|
}
|