const Benchmark = require('benchmark');
const Suite = new Benchmark.Suite;
const { parse, URL } = require('url');
const urlObj = (str) => {
try {
return new URL(str);
} catch (e) {
return str;
}
};
// add tests
Suite.add('Legacy URL parse()', () => {
parse('https://skk.moe/path/to/something');
parse('/path/to/something');
}).add('WHATWG URL API', () => {
new URL('https://skk.moe/path/to/something');
new URL('/path/to/something', 'https://skk.moe/');
}).add('safe WHATWG URL', () => {
urlObj('https://skk.moe/path/to/something');
urlObj('/path/to/something');
}).on('cycle', function(event) {
console.info(String(event.target));
}).on('complete', function() {
console.info('Fastest is ' + this.filter('fastest').map('name'));
}).run();