-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Collection#set style (Backwards Compatible) #3663
Collection#set style (Backwards Compatible) #3663
Conversation
…-2-1 Collection#add shouldn't parse a model instance
Mirrors the change in jashkenas#3512.
Splicing in a loop is the worst thing you can do: http://jsperf.com/insert-array-into-another-array/4
- [Base case (`#unshift`)](http://jsperf.com/insert-array-into-another-array/6) - ["Normal" collection size](http://jsperf.com/insert-array-into-another-array/8) - [Large collection](http://jsperf.com/insert-array-into-another-array/7)
84b8071
to
0052c2f
Compare
var singular = !_.isArray(models); | ||
models = singular ? (models ? [models] : []) : models.slice(); | ||
var id, model, attrs, existing, sort; | ||
models = singular ? [models] : _.clone(models); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is clone
faster here? Shouldn't it be quicker to avoid the logic branch in clone?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, didn't even notice I changed that.
👍 |
@@ -1565,7 +1565,7 @@ | |||
collection.on('sort', function() { | |||
ok(true); | |||
}); | |||
collection.set([{id: 3}, {id: 2}, {id: 1}, {id: 0}]); | |||
collection.set([{id: 1}, {id: 2}, {id: 3}, {id: 0}]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Adding a model should trigger a sort". Seemed better to only add a model, as the mixed order case is handled in the test above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair nuf. maybe split this out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split it into what?
Changing |
Collection#set style (Backwards Compatible)
This is a backwards compatible version of #3648.
var i
declarations)models
is nullishat
an order of magnitude faster#unshift
)#push
)Depending on how much performance is an issue, we can use an even faster splice algorithm (also included in the jsperfs), it's just a bit complicated.