Description
-
Version:
- 10.17.0
- 12.13.0
- 13.1.0
-
Subsystem:
url
I have encountered performance regression when mitigating deprecated Legacy URL API to newer recommended WHATWG URL API for a performance-sensitive application. I have set up a benchmark: https://runkit.com/sukkaw/5dc5924f1ba0d60013d87cb3
const Benchmark = require('benchmark');
const Suite = new Benchmark.Suite;
// const { URL } = require('url') is only needed for Nodejs 8.
const { parse, URL } = require('url');
const safeWhatwgUrl = (str) => {
// Handle relative path
try {
return new URL(str);
} catch (e) {
return str;
}
};
Suite.add('Absolute URL - Legacy URL parse()', () => {
parse('https://skk.moe/path/to/something');
}).add('Absolute URL - WHATWG URL API', () => {
new URL('https://skk.moe/path/to/something');
}).add('Absolute URL - safe WHATWG URL', () => {
// Should be very slow, since try...catch is very expensive
safeWhatwgUrl('https://skk.moe/path/to/something');
}).add('Relative Path - Legacy URL parse()', () => {
parse('/path/to/something');
}).add('Relative Path - WHATWG URL API', () => {
// pass a `base` to handle relative path
new URL('/path/to/something', 'https://skk.moe/');
}).add('Relative Path - safe WHATWG URL', () => {
// Should be even slower.
safeWhatwgUrl('/path/to/something');
}).on('cycle', function(event) {
console.info(String(event.target));
}).run();
Resulted in:
Node.js 8.16.2
"Absolute URL - Legacy URL parse() x 214,035 ops/sec ±1.94% (84 runs sampled)"
"Absolute URL - WHATWG URL API x 182,604 ops/sec ±0.62% (88 runs sampled)"
"Absolute URL - safe WHATWG URL x 162,791 ops/sec ±3.63% (83 runs sampled)"
"Relative Path - Legacy URL parse() x 1,008,555 ops/sec ±3.55% (74 runs sampled)"
"Relative Path - WHATWG URL API x 70,292 ops/sec ±2.69% (80 runs sampled)"
"Relative Path - safe WHATWG URL x 55,500 ops/sec ±2.62% (85 runs sampled)"
On Node.js 8.16.2, The WHATWG URL API is 15% slower than Legacy URL API when handle a normal URL. It seems normal and acceptable.
Node.js 10.17.0
"Absolute URL - Legacy URL parse() x 483,508 ops/sec ±1.00% (93 runs sampled)"
"Absolute URL - WHATWG URL API x 286,201 ops/sec ±1.11% (93 runs sampled)"
"Absolute URL - safe WHATWG URL x 272,538 ops/sec ±1.00% (93 runs sampled)"
"Relative Path - Legacy URL parse() x 2,127,003 ops/sec ±0.46% (95 runs sampled)"
"Relative Path - WHATWG URL API x 169,296 ops/sec ±0.58% (96 runs sampled)"
"Relative Path - safe WHATWG URL x 96,357 ops/sec ±0.11% (98 runs sampled)"
On Node.js 10.17.0, The WHATWG URL API is 40% slower than Legacy URL API when handle a normal URL.
Node.js 12.13.0
"Absolute URL - Legacy URL parse() x 393,703 ops/sec ±1.21% (89 runs sampled)"
"Absolute URL - WHATWG URL API x 225,574 ops/sec ±0.59% (92 runs sampled)"
"Absolute URL - safe WHATWG URL x 208,342 ops/sec ±0.98% (90 runs sampled)"
"Relative Path - Legacy URL parse() x 1,513,601 ops/sec ±3.66% (84 runs sampled)"
"Relative Path - WHATWG URL API x 114,788 ops/sec ±1.29% (92 runs sampled)"
"Relative Path - safe WHATWG URL x 10,425 ops/sec ±0.99% (89 runs sampled)"
On Node.js 12.13.0, The WHATWG URL API is 42% slower than Legacy URL API when handle a normal URL.
Node.js 13.1.0
"Absolute URL - Legacy URL parse() x 345,345 ops/sec ±0.94% (87 runs sampled)"
"Absolute URL - WHATWG URL API x 212,058 ops/sec ±0.41% (91 runs sampled)"
"Absolute URL - safe WHATWG URL x 200,212 ops/sec ±0.76% (92 runs sampled)"
"Relative Path - Legacy URL parse() x 1,407,585 ops/sec ±1.72% (89 runs sampled)"
"Relative Path - WHATWG URL API x 108,171 ops/sec ±0.51% (90 runs sampled)"
"Relative Path - safe WHATWG URL x 10,279 ops/sec ±0.57% (90 runs sampled)"
On Node.js 13.1.0, The WHATWG URL API is 38% slower than Legacy URL API when handle a normal URL.
And when I try to pass a base
to handle relative path, it becomes even more slower. I believe it is because I pass a string to base
so The WHATWG URL have to parse base
first.