过滤离散点性能优化记录

66 阅读1分钟
  1. 单项的index从string变为number以下方法耗时减少近半
    _getTargetByIndex(points,index){
        for(const p of points){
            if(p.index===index){
                return p
            }
        }
        return null
    }
    
    const added = arr.includes(index)
    
  2. 查找数据量很大时,可以把查询结果缓存起来
    function getTarget(index){
        let target = cache[index]
        if(target){
            return {added:true,target}
        }else{
            target = _getTargetByIndex(points,index)
            cache[index]= target
            return {added:false,target}
        }
    }
    
  3. 过滤离散点
    while(itemIndex !== undefined){
        const {target:item}=getTarget(itemIndex)
        if(!item) continue
        for(const index of item.neighbor){
            const {target,added} = getTarget(idnex)
            if(target && added){
                arr.push(index)
            }
        }
    }