webreflection / udomdiff Goto Github PK
View Code? Open in Web Editor NEWAn essential diffing algorithm for µhtml.
Home Page: https://medium.com/@WebReflection/the-web-smallest-dom-diffing-library-5b69ac4d1f4d
License: ISC License
An essential diffing algorithm for µhtml.
Home Page: https://medium.com/@WebReflection/the-web-smallest-dom-diffing-library-5b69ac4d1f4d
License: ISC License
Hi, I really like domdiff and found out about udomdiff recently.
I also saw that udomdiff is more performant and smaller but does not provide a compare function, like domdiff does.
So I replaced lines https://github.com/WebReflection/udomdiff/blob/master/index.js#L59,
https://github.com/WebReflection/udomdiff/blob/master/index.js#L63 and https://github.com/WebReflection/udomdiff/blob/master/index.js#L69 with the compare Function and run into Problems with insertBefore on line 49.
How would it be possible to add it again?
@WebReflection I'm not even sure if this package is the current?, you have a million of packages xD
anyway, the question is: can the algorithm be generalized? This is also useful for merging stores, albeit I am not really that sure about that. Solid seems to use some versions, or modifications of this algo, in different situations and I am wondering if can be generalized, because I am currently dealing with the same thing, I need to patch the dom, and I also need to patch stores.
https://github.com/solidjs/solid/blob/dbdc27df2246ec05c0e32cf7b461ddbe4223915f/packages/solid/store/src/modifiers.ts#L3
maybe not all diffing, because this
https://github.com/solidjs/solid/blob/dbdc27df2246ec05c0e32cf7b461ddbe4223915f/packages/solid/src/reactive/array.ts
seems to come from
https://github.com/adamhaile/S-array
https://github.com/ryansolid/dom-expressions/blob/main/packages/dom-expressions/src/reconcile.js
Do you see any approach to generalise this problem?
Thanks!
Would be possible to add some benchmarks to the repo if you have the code ? I have tried udomdiff a couple of times but surprisingly it's slightly slower. Which is highly suspicious or what I am doing is just faster in some situations. Even if I don't use I would love to give ideas to improve its speed when in scope of what you expect from this lib.
With some benchmark added I could test implementations and show off situations that you may consider improving. For example if diffing is only about removals this may be faster(or maybe not) but you will get an idea of what I am thinking
export function remove(prev = [], node = []) {
for (let i = 0, item; i < prev.length; i++) {
item = prev[i]
item &&
(node.length === 0 || !node.includes(item)) &&
item.remove()
}
return node
}
I wasn't sure if it's a bug but I put together a benchmark comparing some diffing algorithms out there (based on your test code, thank you!).
udomdiff has 199 operations on updating the 10th row compared to the ideal 100.
Hey,
I've been testing out some diffing algos, curious to see which DOM operations they actually perform.
One of the libraries I tested was this one, and I've come across a case where it seems to do an unnecessary DOM operation.
Here's my test bench:
https://jsfiddle.net/mindplay/o4gbaqw0/
I've added console.log
statements for every DOM operation - you can press the buttons to perform various array operations to change the sorting of the elements, and then watch the console output for the exact DOM operations resulting from the diff operation from one state to the next.
It looks good for all operations except "Rotate Up" - which basically means, "take the first item on the list and move it to the bottom", which should be doable with a single insertBefore
.
Instead, the result is two operations - a seemingly unnecessary replaceChild
operation, followed by the expected insertBefore
operation. (The resulting DOM state is correct though.)
With devtools open, you can see two nodes are being affected:
It isn't explicitly stated in the README of either this project or the original domdiff
, but presumably the point of diffing is to perform as few DOM operations as possible? Just figured I'd report my findings, in case you do consider this a subtle "bug". 🙂
The function udomdiff
applies changes directly. Is there a way that I can get the diff (serializable as JSON) and then later apply the diff?
pseudocode demostrating the concept:
diff = get_patch(newnode, oldnode)
apply_patch(oldnode, diff)
This will be useful when there are two copies of oldnode
(DOM) on server and browser respectively.
Then, I can patch uhtml render(oldnode, html`...`)
(run on server) to update the browser DOM. Event handlers is a bit tricky, and can theoretically be diffed as well.
This is really an awesome piece of code! Congrats!
I'm trying to incorporate it in sinuous/map
.
I was wondering about a use case when an item is a document fragment.
I see it works nicely with arrays.
what do you recommend for handling fragments?
convert the list items to arrays first or use something like uwire?
thanks!
domdiff
has a simple test before removing a node from its parent. udomdiff
doesn't check the existence of the property parentNode
on a node before removing it from the DOM.
Is this behavior intentional or is it a bug?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.