-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a Set for checking if an element is a void element, which is faster than object access #1468
Use a Set for checking if an element is a void element, which is faster than object access #1468
Conversation
It's slower on my machine. |
good idea. with non-existent-keys It looks as if object wins out again. so I'm going to leave this as an object. |
|
that's curious. which makes for an interesting situation with this data structure, because using Map#has, makes the Map basically a Set, but Map performs way better than Set. |
Generally in that package we don’t care about performance that much, at least not to that extent, since it typically only runs during the build, and of all the things that is slow in the build, it is not going to come down to these lookups. So just use whatever reads best and makes the most sense. |
Also if this intended to be public then it’s probably better to export a function to check rather than exposing whatever underlying data structure |
well, I use |
We bundle them to JSON... 😄 |
Yeah then I think we just expose |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me! fwiw, I don't know how "public" anything really is, so at some point, probably when we talk about restructuring the packages to simplify things, we probably want to be more rigorous about delineating the public api surface
I still doubt this is what is a hot path that makes it slow, but I can believe it if it shows up on benchmarks; anyway now that the implementation is private you are more free to tweak that if you find that necessary; if it ends up being that important, then I suspect the answer is neither |
But, in general, the performance profiles of these micro things changes over time and we don't have time to keep up with that, so we just end up with a lot of micro optimizations that we don't know if are still necessary or actively causing problems |
function isVoidTag(tag: string): boolean { | ||
return !!(voidMap[tag.toLowerCase()] && tag[0]?.toLowerCase() === tag[0]); | ||
export function isVoidTag(tag: string): boolean { | ||
return !!(voidMap.has(tag.toLowerCase()) && tag[0]?.toLowerCase() === tag[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!!
become useless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! I wasn't even looking at that -- more ancient code
…t vs Map's perf differences are not important enough right now (since this is build-time code), and maintainability takes precedence in this case
3a23a63
to
f92a036
Compare
Maybe this is a good chance to mention that several void tags actually missing. Someone may interest to in investigate. If they are just missing by mistake, maybe we can switch to use |
And it's funny, three of them removed in HTML spec... https://github.com/wooorm/html-void-elements/pull/7/files |
@chancancode I can do this switch in a separate PR so the PRs stay focused |
|
When it comes to raw speed, for small datasets, Map.get is faster than Object[..] is faster than Set.has
benchmark - Map#get - key exists
benchmark - Map#get - key does not exist
builds on top of: #1467
benchmark - Map#has - key does not exist
benchmark - Map#has - key exists