I'm currently facing a performance issue with the search function in my react-native app. The search involves scanning through an array of 15,000 elements for specific keywords. Surprisingly, the search performs well and quickly on Android devices, but significantly slower on iOS devices. I've tested the app on real Samsung Galaxy S7 and iPhone 6s devices, and the difference in performance is quite noticeable. Here's a snippet of the code I'm using for the search functionality:
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,
});
}
}
Can anyone suggest ways to optimize the search performance for iOS devices?