I was trying to test whether new URL(url)
or new URL(url.href)
would be faster since it’s in a fairly hot code path. The latter came out way faster and after some digging I found out that it’s because the Node.js implementation of URL has a line that converts the input to a string with a template string literal https://github.com/nodejs/node/blob/8bc6e193a07613a29eaf30d609f3c6d20db94031/lib/internal/url.js#L734
I benched just the template string conversion against converting it to a string beforehand with toString and the latter was about 6x faster in Node and 2x faster in Firefox. Why is that?