-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Proposal for list diff algorithm #1807
Comments
Hmm, are you sure? Vue actually performs only 1 |
@yyx990803 Code below is the scenario I described, you can have a try. I put a import Vue from "vue"
var app = new Vue({
el: "#content",
data: {
list: [
{name: "a"},
{name: "b"},
{name: "c"},
{name: "d"},
{name: "e"},
{name: "f"},
{name: "g"},
{name: "h"}
]
}
})
setTimeout(function() {
app.list = [
{name: "b"},
{name: "c"},
{name: "d"},
{name: "e"},
{name: "f"},
{name: "g"},
{name: "h"},
{name: "a"}
]
}, 2333)
Here is the case perform 1 import Vue from "vue"
var app = new Vue({
el: "#content",
data: {
list: [
{name: "a"},
{name: "b"},
{name: "c"},
{name: "d"},
{name: "e"},
{name: "f"},
{name: "g"},
{name: "h"}
]
}
})
setTimeout(function() {
app.list = [
{name: "h"},
{name: "a"},
{name: "b"},
{name: "c"},
{name: "d"},
{name: "e"},
{name: "f"},
{name: "g"}
]
}, 2333) |
@livoras you are replacing the entire list with fresh objects, therefore Vue is removing all old fragments and inserting all the new ones. In fact neither of your examples should call If you add proper identity tracking with |
@yyx990803 yes,I added
|
I tested your exact code locally and got 1 move call. If it's making 7 calls for you, please provide a live demo... |
Ah sorry, I was looking at the second snippet. Yes it is indeed making 7 calls. |
@livoras thanks a lot for the suggestion! It actually optimizes all scenarios when only a single item is moved. |
@yyx990803 Great job~! 👍 It actually optimizes scenarios that non-adjacent items are moved. :) |
According to the
for
directive's diff algorithm, when vue diffs two arrays like:it causes seven manipulations of DOM(the move function is called seven times), but the actual minimal amount is just two (remove
a
from the first place, and insert it to the last).Vue's list diff algorithm is actually good at cases like inserting new items or moving items from back to front/middle. But cases that just simply moving a item from front to back/middle, it will cause a lot manipulations.
I didn't mean to make it minimal manipulations like levenshtein distance based algorithm does. But moving items from front to back/middle is quite common, so I think it's worth to optimalize.
Here is the pseudocode:
It just needs one more
if
check.The text was updated successfully, but these errors were encountered: