Skip to content
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

WIP: Use node id as a hint of which node to compare to #61

Merged
merged 1 commit into from
May 1, 2017

Conversation

finnp
Copy link
Contributor

@finnp finnp commented Apr 24, 2017

Hey,

this pull request tries to implement an algorithm for #8. It should perform well on removal and addition of elements in a list. However for reordering it will be worse, especially if the list of dom nodes is inverted. But I guess there is not much we can do in that case.

The theoretical optimal dom modification would be similar to a "Shortest Edit Script" and quite expensive to perform. This algorithm here has multiple nested loops, but has a linear time.

I haven't yet figured out how "mixed content" should be treated, e.g.

<ul>
    <li id="1">hi</li>
    <li>there</li>
    <li id="2"></li>
</ul>

var oldLength = oldNode.childNodes.length
var length = Math.max(oldLength, newLength)
var newIndex = 0
for (var oldIndex = 0; oldIndex < oldChildren.length; oldIndex++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that oldChildren.length is constant, we can evade doing an extra lookup on every loop:

for (var oldIndex = 0, len = oldChildren.length; oldIndex < len; oldIndex++) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, nope not this one - apparently we modify the length of oldChildren - the next one is actually static tho

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • .length is often optimized in engines too

oldNode.appendChild(retChildNode)
iNew--
function findNewChild () {
for (; newIndex < newChildren.length; newIndex++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too, newChildren has a constant length

})

t.test('remove an element', function (t) {
var a = html`<ul><li id="a"></li><li id="b"></li><li id="c"></li></ul>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also multiline this example like the one above? Think it helps with readability. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to debug this one. If I introduce newlines, there will be TextNodes in between the li and it will not work anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, weirrd haha

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use .childNodes instead of .children then as those ignore text nodes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, you're right .children is correct

@yoshuawuyts
Copy link
Member

Super neato patch! - really digging this 😁

Did some super quick testing, and it doesn't appear to be making much of a difference in choo/example because even though the comparison might be cheaper - we still do the full comparison of all properties (which is the expensive part).

Even though this patch is sound, I wonder how much it'll improve performance. I think where it'll truly shine, is eliminating a class of errors for stateful elements (cached or otherwise) to prevent their placeholder return value (el.isSameNode()) from being rendered on accident.

So I think if we're gonna bench, we should bench with fancy, cached nodes so we actually get a perf benefit from comparing two similar nodes ✨

var oldLength = oldNode.childNodes.length
var length = Math.max(oldLength, newLength)
var newIndex = 0
for (var oldIndex = 0; oldIndex < oldChildren.length; oldIndex++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • .length is often optimized in engines too

})

t.test('remove an element', function (t) {
var a = html`<ul><li id="a"></li><li id="b"></li><li id="c"></li></ul>`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use .childNodes instead of .children then as those ignore text nodes

@juliangruber
Copy link
Contributor

I'm giving this a try in a real world application now that hit exactly this bug, will report back here

@juliangruber
Copy link
Contributor

works for me! 🎆

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okkk, merging!

@yoshuawuyts yoshuawuyts merged commit 84fd0a6 into choojs:master May 1, 2017
@finnp finnp deleted the lists branch May 2, 2017 10:29
@finnp
Copy link
Contributor Author

finnp commented May 2, 2017

I was pretty sure that the TextNodes still caused bugs. Are you sure this was good to merge?

@juliangruber
Copy link
Contributor

I was pretty sure that the TextNodes still caused bugs. Are you sure this was good to merge?

as far as i can tell the bug was only in the test code, right?

@yoshuawuyts
Copy link
Member

yoshuawuyts commented May 2, 2017

Think it might trigger anywhere, wrote a patch for Bel so random whitespace no longer creates text nodes; think this should help (:

edit: writing a patch for yo-yoify next

@yoshuawuyts
Copy link
Member

Ya, confirmed - that patch fixes our tests when made multiline (:

@finnp
Copy link
Contributor Author

finnp commented May 2, 2017

Awesome 👍

@yoshuawuyts
Copy link
Member

And now also shama/yo-yoify#45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants