性能优化

103 阅读1分钟

一、代码优化

1.减少、优化for循环

    for(let i = 0;i<arr.length;i++){...}
    for(let i = 0,len = arr.length;i < len ;i++){...}

2、高频率事件设置防抖、节流

  • 高频率触发事件:mousemove、scroll、resize、mousewheel、keydown、keyup、input

防抖

  • 防抖:指触发事件后 n 秒后才执行函数,如果在 n 秒内又触发了事件,则会重新计算函数执行时间

  • 使用定时器来判断

    1.gif

节流

  • 节流:指连续触发事件但是在 n 秒中只执行一次函数,减少触发频率

  • 使用时间差判断

    1.gif

3、图片宽高度设置