2017-01-10 18:38:59 +01:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
|
|
const url = require('url');
|
|
|
|
const URL = url.URL;
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
2018-11-11 09:44:19 +01:00
|
|
|
type: common.urlDataTypes,
|
2017-01-10 18:38:59 +01:00
|
|
|
method: ['legacy', 'whatwg'],
|
2018-11-11 09:44:19 +01:00
|
|
|
e: [1]
|
2017-01-10 18:38:59 +01:00
|
|
|
});
|
|
|
|
|
2018-11-11 09:44:19 +01:00
|
|
|
function useLegacy(data) {
|
|
|
|
const obj = url.parse(data[0]);
|
|
|
|
const len = data.length;
|
2017-01-10 18:38:59 +01:00
|
|
|
var noDead = url.format(obj);
|
|
|
|
bench.start();
|
2018-11-11 09:44:19 +01:00
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
noDead = data[i].toString();
|
2017-01-10 18:38:59 +01:00
|
|
|
}
|
2018-11-11 09:44:19 +01:00
|
|
|
bench.end(len);
|
2017-01-10 18:38:59 +01:00
|
|
|
return noDead;
|
|
|
|
}
|
|
|
|
|
2018-11-11 09:44:19 +01:00
|
|
|
function useWHATWG(data) {
|
|
|
|
const obj = new URL(data[0]);
|
|
|
|
const len = data.length;
|
2017-01-10 18:38:59 +01:00
|
|
|
var noDead = obj.toString();
|
|
|
|
bench.start();
|
2018-11-11 09:44:19 +01:00
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
noDead = data[i].toString();
|
2017-01-10 18:38:59 +01:00
|
|
|
}
|
2018-11-11 09:44:19 +01:00
|
|
|
bench.end(len);
|
2017-01-10 18:38:59 +01:00
|
|
|
return noDead;
|
|
|
|
}
|
|
|
|
|
2018-11-11 09:44:19 +01:00
|
|
|
function main({ type, e, method }) {
|
|
|
|
e = +e;
|
|
|
|
const data = common.bakeUrlData(type, e, false, false);
|
2017-01-10 18:38:59 +01:00
|
|
|
|
|
|
|
var noDead; // Avoid dead code elimination.
|
|
|
|
switch (method) {
|
|
|
|
case 'legacy':
|
2018-11-11 09:44:19 +01:00
|
|
|
noDead = useLegacy(data);
|
2017-01-10 18:38:59 +01:00
|
|
|
break;
|
|
|
|
case 'whatwg':
|
2018-11-11 09:44:19 +01:00
|
|
|
noDead = useWHATWG(data);
|
2017-01-10 18:38:59 +01:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-23 13:18:12 +01:00
|
|
|
throw new Error(`Unknown method ${method}`);
|
2017-01-10 18:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.ok(noDead);
|
|
|
|
}
|