我在我的react-native应用程序中有一个搜索功能,我在其中搜索包含15,000个元素的数组中的关键字。当我在Android设备上执行搜索时,性能良好且足够快。但是,当我在iOS设备上执行相同的操作时,性能比android还要慢。我已经在实际设备上尝试了该应用的发行版,但结果是相同的。如果搜索功能在Android设备上花费1秒钟,则在iOS设备上花费4-5秒钟。我在三星银河S7和iPhone 6s上尝试过。我知道设备的处理器确实很重要,但区别很大。这是我的代码:
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,
});
}
}
关于如何改善搜索性能的任何建议?
答案 0 :(得分:1)
You could create a swift module for this operation to run faster.
Here you can find the official tutorial on the react native website.
I did this before when I needed more performance on a local database and it is a great way to learn how react native works on ios at least.