我公司的一位前开发人员编写了一个 Vue.js typeahead 组件,在我进入 Internet Explorer 11 之前,一切似乎都很好。在绑定了keyup
函数的输入中键入时,它有时不接受用户正在输入的字符。这似乎是我正在使用的代码的性能问题。下面是导致问题的代码,如果我将其全部删除并且什么都不做,则输入没有问题。我可以提出任何性能建议来加快速度吗?
searchResults: function(e){
this.isShown = true
this.selectedIndex = 0
this.filterOptions()
if(this.wildcardSearch){
var searchObj = {}
searchObj[this.displayProp] = 'Search For: <strong>'+this.search+'</strong>'
this.matches.unshift(searchObj)
}
// Show first 5 results
this.matches = this.matches.splice(0,5)
},
filterOptions: function(){
var lowSearch = this.search.toLowerCase()
var that = this
if (this.search) // don't filter on empty string
this.matches = this.options.map(function(o){
if (o[that.displayProp].toLowerCase().indexOf(lowSearch) > -1)
return o
}).filter(function(o){return o})
},
使用的
map
和filter
都是多余的,特别是因为map
被用作filter
而filter
并没有真正做任何事情。
这个呢:
this.matches = this.options.filter(function(o) {
return o[that.displayProp].toLowerCase().indexOf(lowSearch) > -1
})
也就是说,我不明白为什么这会导致性能影响,但这确实是我看到的唯一问题(如果可以的话,我会把它写成评论(。