0.5.3 to 0.9.2, adding to collection inefficiencies

I upgraded backbone.js from 0.5.3 to 0.9.2, and I noticed a significant decrease in speed in my application. The application processes many large collections and adds a large number of models at certain points. In any case, from 0 to 600 models are added. In version 0.5.3, the sortedIndex function was most often used for 12% of the CPU. in version 0.9.2, the code became much slower, and the sortBy function took up 70% of the CPU.

I suspect that they add all models by sorting them, rather than adding each model to where it should contain the collection sort. Is there a flag that I can use to use the old method or any other way to speed it up. I understand that I can implement my own collection class specific to my large datasets, but I would prefer to stick with basic collections right now.

Here is the comparator of the collection

comparator: function(model) {
    return model.get("timestamp");
}

Thanks in advance

+3
source share
1 answer

You can try using two arguments comparator:

sortBy ( , ) sort ( , ).

sort, Underscore sortBy; sortBy _.pluck _.map Schwartzian Transform; , , m.get(a) m.attributes[a] ( get ) .

, :

comparator: function(a, b) {
    if(a.attributes.timestamp < b.attributes.timestamp)
        return -1;
    else if(a.attributes.timestamp > b.attributes.timestamp)
        return 1;
    return 0;
}

Collection#add :

for (i = 0, length = models.length; i < length; i++) {
  //...
  if (this.comparator && options.at == null) this.sort({silent: true});
  //...
}

, at; "add" , , . , add , sort add. , , : ( ) , , .

+3

All Articles