Uh oh!
There was an error while loading. Please reload this page.
url: avoid instanceof for WHATWG URL - #11690
Conversation
joyeecheung
left a comment
There was a problem hiding this comment.
I vaguely remember some of the slowdowns of instanceof are already fixed in the v8 lkgr fork(5.8-ish)...at least for Buffer last time I played around with it. Nonethess I think testing for internal symbols is better anyway.
There was a problem hiding this comment.
The searchParams symbol is used by the URL class as well, so this will no longer error out on URL objects.
There was a problem hiding this comment.
I've now added per-class symbols that are checked instead.
mscdex
commented
Mar 6, 2017
CI again after symbol check changes: https://ci.nodejs.org/job/node-test-pull-request/6718/ |
There was a problem hiding this comment.
I'd prefer urlInstance just to be consistent with searchParamsInstance.
There was a problem hiding this comment.
I'm still reluctant about writing out this[searchParamsInstance] !== true when a !this[searchParamsInstance] would do. Is this change visible performance-wise?
There was a problem hiding this comment.
Just being more explicit about the expected value.
There was a problem hiding this comment.
Why not use the existing context and searchParams symbols rather than introducing new ones? All URL objects should have the context symbol set. Likewise all URLSearchParams instances should have the searchParams symbol set (I believe)
There was a problem hiding this comment.
As @TimothyGu noted, both classes use both of those symbols, so they don't currently each have a unique symbol?
There was a problem hiding this comment.
You can probably separate them like this:
if(url[searchParams]){if(url[searchParams][searchParams]){}// URL, it's an arrayelse{}// URLSearchParams, it's undefined}There was a problem hiding this comment.
I might put the urlInstance in the prototype to avoid having to set this every single time an object is created.
There was a problem hiding this comment.
Then URL.prototype will pass the brand check, which seems bad.
mscdex
commented
Mar 29, 2017
Alright, I've switched to checking using only Results are pretty close to what they were before. |
jasnell
left a comment
There was a problem hiding this comment.
Generally LGTM with a question/suggestion
| input = `${input}`; | ||
| if (base !== undefined && !(base instanceof URL)) | ||
| if (base !== undefined && | ||
| (!base[searchParams] || !base[searchParams][searchParams])) { |
There was a problem hiding this comment.
@mscdex ... is there benefit to moving this into a separate isURL(u) function that can be exported by internal/url?
There was a problem hiding this comment.
Performance-wise? No. There's actually a small but measurable performance hit when extracting all of the logic to a separate function, compared to having it inline. For example:
improvement confidence p.value
url/url-searchparams-read.js n=20000000 param="nonexistent" method="get" 165.07 % *** 1.189398e-66
url/url-searchparams-read.js n=20000000 param="nonexistent" method="getAll" 139.46 % *** 4.331942e-46
url/url-searchparams-read.js n=20000000 param="nonexistent" method="has" 161.48 % *** 2.741360e-52
url/url-searchparams-read.js n=20000000 param="one" method="get" 222.70 % *** 3.111318e-46
url/url-searchparams-read.js n=20000000 param="one" method="getAll" 113.31 % *** 1.122968e-53
url/url-searchparams-read.js n=20000000 param="one" method="has" 232.04 % *** 1.571222e-55
url/url-searchparams-read.js n=20000000 param="three" method="get" 184.13 % *** 2.078669e-48
url/url-searchparams-read.js n=20000000 param="three" method="getAll" 100.34 % *** 4.591714e-56
url/url-searchparams-read.js n=20000000 param="three" method="has" 181.29 % *** 7.340053e-43
url/url-searchparams-read.js n=20000000 param="two" method="get" 209.77 % *** 4.581349e-54
url/url-searchparams-read.js n=20000000 param="two" method="getAll" 102.89 % *** 2.001755e-36
url/url-searchparams-read.js n=20000000 param="two" method="has" 203.09 % *** 1.810527e-44
joyeecheung
commented
Mar 30, 2017
jasnell
commented
Apr 4, 2017
TimothyGu
commented
Apr 5, 2017
Landed in b76a350. |
PR-URL: #11690 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
italoacasas
commented
Apr 10, 2017
cc @mscdex |
guybedford
commented
Aug 28, 2018
mscdex
commented
Aug 28, 2018
@guybedford I do not know offhand. Someone would have to be sure to run benchmarks on both implementations and compare the results for master. |
devsnek
commented
Aug 28, 2018
I think there's also a concern about |
This PR replaces
instanceofchecks with symbol checks. Various relevant results:CI: https://ci.nodejs.org/job/node-test-pull-request/6707/
/cc @nodejs/url
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)