PDA

View Full Version : Why doesn't Store.getById() utilize MixedCollection.getByKey() anymore?



Dipish
19 Jun 2012, 1:12 AM
It used to be like that in Ext 3 but for some mysetrious reason this was changed in Ext 4.0 and remains in Ext 4.1.

So this is how Store.getById() (http://docs.sencha.com/ext-js/4-0/source/Store.html#Ext-data-Store-method-getById) currently looks like in 4.x:

getById: function(id) {
return (this.snapshot || this.data).findBy(function(record) { // OMG!
return record.getId() === id;
});
}

Where MixedCollection.findBy() (http://docs.sencha.com/ext-js/4-0/source/AbstractMixedCollection.html#Ext-util-AbstractMixedCollection-method-findBy) basically iterates through all the records sequentially calling the test function on each one. Obviously, the performance of this method potentially decreases with the number of records.

At the same time we have MixedCollection.getByKey() (http://docs.sencha.com/ext-js/4-0/source/AbstractMixedCollection.html#Ext-util-AbstractMixedCollection-method-getByKey) that looks like this:

getByKey : function(key) {
return this.map[key]; // that's what map is for!
}

Which gives us constantly fast seek time regardless of the number of records.

I was very surprised to see Store.getById() implementation change like that in 4.x, this kills all the benefits of having id index in the store!!! I do not expect the method to work like that.

PLEASE, explain why this has changed and correct me at any part where I'm wrong! Otherwise, please issue a fix ASAP.

Dipish
19 Jun 2012, 1:42 AM
Here is a simple override that I suggest, still don't know if there are any caveats...

Ext.override(Ext.data.store, {
getById: function(id) {
return (this.snapshot || this.data).getByKey(id);
}
});

Dipish
19 Jun 2012, 2:19 AM
Answering my own question.

This implementation:
return (this.snapshot || this.data).findBy(function(record) {
return record.getId() === id;
});

allows us to use strict comparison for record id values, while
return this.map[key]; will perform loose comparison (e.g. '1' == 1 but '1' !== 1).

I realize this can be important but personally I don't see a practical value of using strict comparison here.
If you have two records in a store and one has id=1 and the other has id='1' then you're likely doing something wrong.

I would rather have fast record seek time rather than strict comparison for the sake of unknown potential benefits, so I'm using my override. But I'd love to hear comments on this from experts. Is this a bad idea in the long run?