I have a search function in my react-native app where I am searching keywords in an array of 15,000 elements. When I am performing the search on an Android device, the performance is good and fast enough. However, when I am performing the same action on an iOS device, the performance is way too slower than android. I have tried the release version of the app on real devices but the result was the same. If the search function is taking 1 sec on an android device, it's taking 4-5 sec on iOS devices. I tried it on Samsung galaxy S7 and iPhone 6s. I know the processor of the device does matter but the difference is significant. Here is my code:
let query = this.state.searchQuery;
query = query.toString().toLowerCase();
let lastChar = query.substr(query.length - 1);
if (query.length !== 0 && lastChar !== ' ') {
let self = this.state.allProductData;
let keywords = query.split(" ");
this.setState({
keywordsSearching: keywords
});
if (keywords.length !== 0) {
let arr = [];
for (var index = 0, selfLen = self.length; index < selfLen; index++) {
if (!this.state.isSearching) {
break;
}
let counter = 0;
var obj = self[index];
let product_name = (obj.product_name).toString().trim().replace(/ /g, '').toUpperCase();
let product_code = (obj.product_code).toString().trim().replace(/ /g, '').toUpperCase();
let product_desc = (obj.product_desc).toString().trim().replace(/ /g, '').toUpperCase();
for (var i = 0, len = keywords.length; i < len; i++) {
var key = keywords[i];
key = key.toString().toUpperCase();
if ((product_name.search(key)) !== -1 || (product_code.search(key)) !== -1 || (product_desc.search(key)) !== -1) {
counter++;
} else {
counter--;
}
}
if (counter > 0 && counter >= keywords.length) {
if (obj.product_id !== undefined) {
arr.push(obj);
}
}
};
this.setState({
isSearching: false,
searchResult: arr,
});
}
}
Any suggestion on how I can improve the search performance?