最近,我一直在使用两种方法在 React 中绑定(bind)它。
handleChange = (e) => {
this.setState({
newData: e.target.value
})
}
对比
handleChange(e) => {
...
}
但需要 const this.handleChange = this.handleChange.bind(this)
哪个性能更好?谢谢。
最佳答案
没有 bind
的版本很可能更快。自从 ES5 出现以来,bind
就被认为是性能问题的根源,因为标准要求 bind
处理所有边缘情况。
基准测试 bind
与使用闭包的对比表明 bind
很可能慢很多倍,可以看出 here .由于箭头函数只是闭包,在这种情况下箭头函数几乎肯定会更快。
关于javascript - 在 React 中绑定(bind)到 this 的性能最高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647167/