【进阶 6-5 期】[译] Throttle 和 Debounce 在 React 中的应用

本文探讨了在React应用中如何利用Throttle和Debounce技术提升性能,避免频繁的API请求和DOM更新导致的问题。通过实例解释了Throttle和Debounce的区别,展示了如何使用underscore、lodash、RxJS进行实现,并提供了自定义实现的方法。文章还强调了在游戏等场景中这两个技术的重要性,以减少不必要的资源消耗,提高用户体验。
摘要由CSDN通过智能技术生成
 
 

本篇是译文,原文是 Improve Your React App Performance by Using Throttling and Debouncing

引言

使用 React 构建应用程序时,我们总是会遇到一些限制问题,比如大量的调用、异步网络请求和 DOM 更新等,我们可以使用 React 提供的功能来检查这些。

在这篇文章中,我们将研究如何在不使用 React 提供的功能下来改进 React 应用程序性能,我们将使用一种不仅仅适用于 React 的技术:节流(Throttle)和防抖(Debounce)。

从一个例子开始

例子 1

下面这个例子可以很好的解释节流和防抖带给我们的好处,假设我们有一个 autocomp 组件

import React from 'react';import './autocomp.css';from 'react';
import './autocomp.css';
class autocomp extends React.Component {    constructor(props) {        super(props);        this.state= {            results: []        }    }
    constructor(props) {
        super(props);
        this.state= {
            results: []
        }
    }
    handleInput = evt => {        const value = evt.target.value        fetch(`/api/users`)            .then(res => res.json())            .then(result => this.setState({ results: result.users }))    }
        const value = evt.target.value
        fetch(`/api/users`)
            .then(res => res.json())
            .then(result => this.setState({ results: result.users }))
    }
    render() {        let { results } = this.state;        return (            <div className='autocomp_wrapper'>            <input placeholder="Enter your search.." onChange={this.handleInput} />            <div>                {results.map(item=>{item})}            </div>            </div>        );    }}export default autocomp;let { results } = this.state;
        return (
            <div className='autocomp_wrapper'>
            <input placeholder="Enter your search.." onChange={this.handleInput} />
            <div>
                {results.map(item=>{item})}
            </div>
            </div>
        );
    }
}
export default autocomp;

在我们的 autocomp 组件中,一旦我们在输入框中输入一个单词,它就会请求 api/users 获取要显示的用户列表。 在每个字母输入后,触发异步网络请求,并且成功后通过 this.setState 更新DOM。

现在,想象一下输入 fidudusola 尝试搜索结果 fidudusolanke,将有许多名称与 fidudusola 一起出现。

1.  f2.  fi3.  fid4.  fidu5.  fidud6.  fidudu7.  fidudus8.  fiduduso9.  fidudusol10. fidudusola
2.  fi
3.  fid
4.  fidu
5.  fidud
6.  fidudu
7.  fidudus
8.  fiduduso
9.  fidudusol
10. fidudusola

这个名字有 10 个字母,所以我们将有 10 次 API 请求和 10 次 DOM 更新,这只是一个用户而已!! 输入完成后最终看到我们预期的名字 fidudusolanke 和其他结果一起出现。

即使 autocomp 可以在没有网络请求的情况下完成(例如,内存中有一个本地“数据库”),仍然需要为输入的每个字符/单词进行昂贵的 DOM 更新。

const data = [    {        name: 'nnamdi'    },    {        name: 'fidudusola'    },    {        name: 'fashola'    },    {        name: 'fidudusolanke'    },    // ... up to 10,000 records]
    {
        name'nnamdi'
    },
    {
        name'fidudusola'
    },
    {
        name'fashola'
    },
    {
        name'fidudusolanke'
    },
    // ... up to 10,000 records
]
class autocomp extends React.Component {    constructor(props) {        super(props);        this.state= {            results: []        }    }
    constructor(props) {
        super(props);
        this.state= {
            results: []
        }
    }
    handleInput = evt => {        const value = evt.target.value        const filteredRes = data.filter((item)=> {            // algorithm to search through the `data` array        })        this.setState({ results: filteredRes })    }
        const value = evt.target.value
        const filteredRes = data.filter((item)=> {
            // algorithm to search through the `data` array
        })
        this.setState({ results: filteredRes })
    }
    render() {        let { results } = this.state;        return (            <div className='autocomp_wrapper'>                <input placeholder="Enter your search.." onChange={this.handleInput} />                <div>                    {results.map(result=>{result})}                </div>            </div>        );    }}let { results } = this.state;
        return (
            <div className='autocomp_wrapper'>
                <input placeholder=<
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值