Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I use a ~100 line long DOM diffing algorithm

Since this is not vDOM diffing (which most JS frameworks nowadays provide) can you help share the algorithm if its already out in the open / non-proprietary?



It's really nothing special. I wrote the whole thing in, like, a single afternoon. If you're interested, here it is:

https://pastebin.com/V7UiWj1e

It's not really a general purpose diffing algorithm. It probably doesn't handle every corner case as it only does what I need it to do in the few places I actually need it.


Would you even need diffing if, for example, the relevant nodes had an ID functionally derived from their contents?


Sorry, I'm not sure I understand your question?

Basically, I use diffing because it's simple. I don't want to mess around with manually updating whatever content I want to update, and I don't want to be forced to generate a specially crafted piece of HTML so that it's updatable. This way I only have to have only one function that updates the current page in-place by diffing, and I can use it everywhere I need to.


I meant that the old tree and the new tree both originate from the same source (i.e., your code), and if both were annotated with some kind of hashes of their contents, you wouldn't need any diffing to find the changes. You could simply compare the annotations on corresponding pairs of nodes and if they're the same, you don't need to descend any further and compare their contents - you already know than anything below that node is going to be identical.

Also, have you compared the performance of whatever you're doing with a simple PJAX-like approach? Considering that browsers are very good at parsing HTML, I wonder how fast that would be compared to doing quite a lot of DOM calls, which you seem to be doing (?). I'd definitely be interested in that comparison.


> if both were annotated with some kind of hashes of their contents

That's exactly what I don't want to do, since it's extra complexity and extra work on my part. (:

> Also, have you compared the performance of whatever you're doing with a simple PJAX-like approach? Considering that browsers are very good at parsing HTML, I wonder how fast that would be compared to doing quite a lot of DOM calls, which you seem to be doing (?).

Nope, sorry, I haven't done any benchmarks. From what I've tested the performance is great. If the pages where I use it were heavier then maybe I'd have performance problems, but so far I don't.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: