如何加快阵列搜索功能?

时间:2018-04-22 08:07:26

标签: javascript arrays react-native search expo

我正在使用react-native编写的字典应用程序。

当我想从搜索框中过滤数组时,我写了下面的函数。当我使用2000单词列表进行测试时,这工作得非常好。但是当单词列表达到数千时,搜索速度确实很慢。

那么,我该如何改进这个搜索功能?

//Filter array when input text (Search)

let filteredWords = []
if(this.state.searchField != null)
{
  filteredWords = this.state.glossaries.filter(glossary => {
    return glossary.word.toLowerCase().includes(this.state.searchField.toLowerCase());
  })
}

2 个答案:

答案 0 :(得分:2)

由于问题doesn't似乎属于CodeReview,我认为您可以采取一些措施使您的代码速度更快[需要引证]:

  • 调用this.state.searchField.toLowerCase()的缓存,因为您不需要在每次迭代时调用它。
  • 使用常规的旧for循环代替华而不过Array函数。

以下是最终结果:

let filteredWords = []
if(this.state.searchField != null) {
    let searchField = this.state.searchField.toLowerCase(),
        theArray = this.state.glossaries;                          // cache this too

    for(let i = 0, l = theArray.length; i < l; ++i) {
        if(theArray[i].word.toLowerCase().includes(searchField)) {
            filteredWords.push(theArray[i]);
        }
    }
}

修改

如果您要搜索wordsearchField开头的词汇表,请使用indexOf === 0代替includes作为这样的条件:

if(theArray[i].word.toLowerCase().indexOf(searchField) === 0) {

答案 1 :(得分:2)

有许多因素导致此代码变慢:

  • 您正在使用带有lambda的filter()。这为每个被搜索的项目添加了函数调用开销。
  • 您在调用toLowercase()之前在两个字符串上调用includes()。这将为每次比较分配两个新的字符串对象。
  • 您正在致电includes。由于某些原因,includes()方法在某些浏览器中的优化程度不如indexOf()

for循环(-11%)

我建议您创建一个新的filter()并使用Array循环来填充它,而不是使用for方法。

const glossaries = this.state.glossaries;
const searchField = this.state.searchField;
const filteredWords = [];   

for (let i = 0; i < glossaries.length; i++) {
  if (glossaries[i].toLowerCase().includes(searchField.toLowerCase())) {
    filteredWords.push(glossaries[i]);
  }
}

toLowerCase分配(-45%)

内存分配很昂贵,因为JavaScript使用垃圾收集机制来释放已用过的内存。执行垃圾收集时,整个程序在尝试查找不再使用的内存时暂停。

每次更新词汇表时,您可以通过复制词汇表来完全摆脱toLowerCase()(在搜索循环内),我认为这种情况并不常见。

// When you build the glossary
this.state.glossaries = ...;
this.state.searchGlossaries = this.state.glossaries.map(g => g.toLowerCase());

您还可以通过在循环之前调用一次来删除searchText上的toLowerCase()。完成这些更改后,代码将如下所示:

const glossaries = this.state.glossaries;
const searchGlassaries = this.state.searchGlossaries;
const searchField = this.state.searchField.toLowerCase();
const filteredWords = []; 

for (let i = 0; i < glossaries.length; i++) {
  if (searchGlassaries[i].includes(searchField)) {
    filteredWords.push(glossaries[i]);
  }
}

indexOf()代替includes()( - 13%)

我不确定为什么会这样,但测试显示indexOfincludes快得多。

const glossaries = this.state.glossaries;
const searchGlassaries = this.state.searchGlossaries;
const searchField = this.state.searchField.toLowerCase();
const filteredWords = []; 

for (let i = 0; i < glossaries.length; i++) {
  if (searchGlassaries[i].indexOf(searchField) !== -1) {
    filteredWords.push(glossaries[i]);
  }
}

总体而言,性能提升了70%。 我从https://jsperf.com/so-question-perf

获得了效果百分比

优化算法

在评论中,您说您希望在需求被放宽以仅匹配以搜索文本开头的单词时可以执行的优化示例。一种方法是binary search

让我们从上面的代码作为起点。我们在将术语表存储到状态之前对它们进行排序。为了不敏感地排序大小写,JavaScript公开了Intl.Collator构造函数。它提供了返回的compare(x, y)方法:

negative value  | X is less than Y
zero            | X is equal to Y
positive value  | X is greater than Y

结果代码:

// Static in the file
const collator = new Intl.Collator(undefined, {
  sensitivity: 'base'
});

function binarySearch(glossaries, searchText) {
  let lo = 0;
  let hi = glossaries.length - 1;

  while (lo <= hi) {
    let mid = (lo + hi) / 2 | 0;
    let comparison = collator.compare(glossaries[mid].word, searchText);

    if (comparison < 0) {
      lo = mid + 1;
    }
    else if (comparison > 0) {
      hi = mid - 1;
    }
    else {
      return mid;
    }
  }

  return -1;
}

// When you build the glossary
this.state.glossaries = ...;
this.state.glossaries.sort(function(x, y) {
  return collator.compare(x.word, y.word);
});

// When you search
const glossaries = this.state.glossaries;
const searchField = this.state.searchField.toLowerCase();
const filteredWords = [];

const idx = binarySearch(glossaries, searchField);

if (idx != -1) {
  // Find the index of the first matching word, seeing as the binary search
  // will end up somewhere in the middle
  while (idx >= 0 && collator.compare(glossaries[idx].word, searchField) < 0) {
    idx--;
  }

  // Add each matching word to the filteredWords
  while (idx < glossaries.length && collator.compare(glossaries[idx].word, searchField) == 0) {
    filteredWords.push(glossaries[idx]);
  }
}