For popular threads with a lot of comments, collapsing a top comment leads to high CPU usage and can cause browsers (I tried in Chrome and Vivaldi) to freeze up.
For example, this very popular thread:
https://news.ycombinator.com/item?id=43561253&utm_term=comment
If you collapse the top comment, I see a huge spike in CPU and my system freezes temporarily.
Is it just me?
The Javascript is surprisingly readable (not minimized, not using any framework). It looks like all the comments are peers according to the DOM structure. When it collapses a comment, it loops through all the following DOM siblings until returning to the same indent level (so all the semantic descendants) and hides them one-by-one. I could see why this could be a bit slow even though it isn't for me. I'm not really a frontend person so I don't know why they might have chosen this structure. One could imagine if the DOM structure were set up such that the comment's descendants were DOM descendants, it wouldn't have to do this at all. It could just use a CSS selector that would hide elements (and thus their descendants also) if their direct parent were collapsed.
reply