Arrays in javascript do not have the indexOf method. extjs adds this method to the data.
The Code is as follows:
Ext. applyIf (Array. prototype ,{
/**
* Checks whether or not the specified object exists in the array.
* @ Param {Object} o The object to check
* @ Param {Number} from (Optional) The index at which to begin the search
* @ Return {Number} The index of o in the array (or-1 if it is not found)
*/
IndexOf: function (o, from ){
Var len = this. length;
From = from | 0;
From + = (from <0 )? Len: 0;
For (; from <len; ++ from ){
If (this [from] === o ){
Return from;
}
});
Return-1;
}
From the source code, we can see that the search is a simple linear search.
Because the linear search efficiency is O (n), when the data volume is slightly larger, we need to find a method to replace Array. Many articles have mentioned this issue about Array, including the authoritative guide, by simulating a Hash table.
Below is the problematic code
The Code is as follows:
Var hostsIP = [];
Ext. each (_ this. hosts, function (item ){
HostsIP. push (item. ip );
});
Ext. each (txtHostsIP, function (ip ){
If (hostsIP. indexOf (ip) ===- 1) {// problem code
Var host = {
IsAppend: true, // new host
IsAgentOk: false,
Ip: ip
};
_ This. hosts. push (
Ext. apply (host, _ this. MAPPING_FIELDS)
);
IsAppend = true;
} Else {
Errors. push ('IP ['+ IP +'] already exists ');
}
});
When the hostsIP length exceeds 2000, the following prompt appears in the IE 8-Browser:
As prompted in the authoritative guide, I fixed the problem after modifying the Code as follows.
The Code is as follows:
Var hostsIP = {};
Ext. each (_ this. hosts, function (item ){
HostsIP [item. ip] = item. ip;
});
Ext. each (txtHostsIP, function (ip ){
If (! HostsIP. hasOwnProperty (ip )){
Var host = {
IsAppend: true, // new host
IsAgentOk: false,
Ip: ip
};
_ This. hosts. push (
Ext. apply (host, _ this. MAPPING_FIELDS)
);
IsAppend = true;
} Else {
Errors. push ('IP ['+ IP +'] already exists ');
}
});