记面试引发的一点思考(遍历性能)

83 阅读1分钟

关于列表搜索

最开始想的是使用filter

 const a = [1,2,3,4,5];
 const result = a.filter(item => {
    return item === 3;
 });

后来被问到map,filter,reduce,find这些有什么性能差异,一时语塞,回答了都差不多。 现在想想,find找到符合条件的项就不会继续遍历数组,性能应该是更优的。

const a = [1,2,3,4,5];
const result = a.find( item =>{ 
   return item === 3
})

完!