# 性能优化总结

性能优化原则

  • 多使用内存 缓存
  • 减少 CPU 计算量,减少网络加载耗时

# 让加载更快

  • 减少资源体积:压缩代码 通过 webpack 配置压缩代码
  • 减少访问次数 合并代码 SSR 服务端徐

# 页面容错处理

  • 后台返回的json数据层级过长,取值的容错处理

let obj = {
    info:{ 
        head:{
            url:''
        }
    }
}
obj.info = null;

console.log(obj.info.head.url);//TypeError: Cannot read property 'head' of null
//容错处理
console.log(obj && obj.info && obj.info.head && obj.info.head.url||'--');
  • 通过lodash.get处理容错
let obj = {
    info:{ 
        head:{
            url:''
        }
    }
}
obj.info = null;
let str = lodash.get(obj, `${obj.info.head.url}.Chinese`, '--'); // 使用 lodash 来简化

  • 通过es2020 ?:来处理容错
const userName = obj?.info?.head?.url || '-';