PDA

View Full Version : Poor performance about Ext.data.Store



haduki
22 Aug 2012, 8:50 PM
Sorry for my poor english.
I find some performance issue about Store.


example1: Ext.data.Store.sync() function



Ext.define('User',{
extend:'Ext.data.Model',
config:{
fields:['id','name']
}
});
Ext.define('UserStore',{
extend:'Ext.data.Store',
config:{
model:'User',
proxy:'memory'
}
});
var userStore=Ext.create('UserStore');
//create data,low performance
var date=new Date();
for(var i=0;i<5000;i++){
var user=Ext.create('User',{id:i,name:"user"+i});
userStore.add(user);
userStore.sync(); //this code cost about 150 sec
}
console.info(['create data:',(new Date().getTime()-date.getTime())/1000,'sec'].join(''));





example2: Ext.data.Store.getById() function

This function search data property 'id' from step by step.
I have to use these code instead of getById().
var idIndex=store.indexOfId(id);
return idIndex>-1?store.getAt(idIndex):null;



Ext.define('User',{
extend:'Ext.data.Model',
config:{
fields:['id','name']
}
});
Ext.define('UserStore',{
extend:'Ext.data.Store',
config:{
model:'User',
proxy:'memory'
}
});
var userStore=Ext.create('UserStore');
//create data
var date=new Date();
for(var i=0;i<5000;i++){
var user=Ext.create('User',{id:i,name:"user"+i});
userStore.add(user);
}
console.info(['create data:',(new Date().getTime()-date.getTime())/1000,'sec'].join(''));
data=new Date();
for(var i=0;i<5000;i++){
userStore.getById(i);
if((i+1) 0==0)
console.info(i+1);
}
console.info(['get data:',(new Date().getTime()-date.getTime())/1000,'sec'].join(''));




example3:add association data.




Ext.define('Pet',{
extend:'Ext.data.Model',
config:{
fields:['id','name','user_id']
}
});
Ext.define('User',{
extend:'Ext.data.Model',
config:{
fields:['id','name'],
hasMany: {model: 'Pet',name:'pets'}
}
});
Ext.define('UserStore',{
extend:'Ext.data.Store',
config:{
model:'User',
proxy:'memory'
}
});
Ext.define('PetStore',{
extend:'Ext.data.Store',
config:{
model:'Pet',
proxy:'memory'
}
});
var petStore=Ext.create('PetStore'),userStore=Ext.create('UserStore');
//create data
var date=new Date();
for(var i=0;i<5000;i++){
var user=Ext.create('User',{id:i,name:"user"+i});
userStore.add(user);
var pet1=Ext.create('Pet',{id:'cat'+i,name:'cat'+i});
var pet2=Ext.create('Pet',{id:'dog'+i,name:'dog'+i});
petStore.add(pet1);petStore.add(pet2);
}
console.info(['create data:',(new Date().getTime()-date.getTime())/1000,'sec'].join(''));
data=new Date();
//each data
for(var i=0;i<5000;i++){
var id=i;
var idIndex=userStore.indexOfId(id);
if(idIndexgt;-1){
var user=userStore.getAt(idIndex);
var pet1=petStore.getAt(petStore.indexOfId('cat'+i));
if(pet1!=null){
//user.pets(); this line code will plus about 7 sec time
}
var pet2=petStore.getAt(petStore.indexOfId('dog'+i));
if(pet2!=null){
//user.pets();
}
}
}
console.info(['each data:',(new Date().getTime()-date.getTime())/1000,'sec'].join(''));
data=new Date();
//association data,very low performance
for(var i=0;i<5000;i++){
var id=i;
var idIndex=userStore.indexOfId(id);
if(idIndex>-1){
var user=userStore.getAt(idIndex);
var pet1=petStore.getAt(petStore.indexOfId('cat'+i));
if(pet1!=null){
user.pets().add(pet1);
}
var pet2=petStore.getAt(petStore.indexOfId('dog'+i));
if(pet2!=null){
user.pets().add(pet2);
}
}
}
console.info(['association data:',(new Date().getTime()-date.getTime())/1000,'sec'].join(''));


sencha touch version: 2.1.0-beta2

mitchellsimoens
24 Aug 2012, 9:09 AM
You shouldn't need to sync() a store using memory proxy. There is no need for it and it really doesn't do anything.

5000 records in a store is quite a lot and there isn't much we can do since this is function iteration. To do just for loops and not execute the function like getById does, you can do this:


date = new Date();

var items = userStore.getData().items,
xLen = items.length,
x, item;

for (i = 0; i < 5000; i++) {
for (x = 0; x < xLen; x++) {
item = items[x];

if (item.getId() == i) {
break;
}
}
}

console.info(['get data 2: ', (new Date().getTime() - date.getTime()) / 1000, 'sec'].join(''));

So the first get data would be about 59 sec where this get data 2 is about 44 sec so is 25% faster and this is really due to the 59 sec run executing all them functions.

Further runs on Chrome got the getById run to 30 sec and the get data 2 run at 13-15 sec which is half the time which is great. Safari on the other hand doesn't do as well going form 50 down to about 39 which is 22% faster.