in my exp I have found that arrow functions do run faster than normal JS functions.
Here is a small snippet in react which uses arrow and normal function. I found that the component using arrow functions runs a bit faster than the one having normal js function.
https://codepen.io/lokeshpathrabe/pen/qgzadx
class Fun extends React.Component {
constructor(props){
super(props);
this.state = {start: new Date().getTime(),
end: new Date().getTime(),
number: 0};
console.log('Function start: ', this.state.start);
const fun = function(me){
let n = me.state.number
me.setState({
...me.state, end: new Date().getTime(), number: ++n
})
}
this.interval = setInterval(fun, 1, this);
}
stop(){
clearInterval(this.interval);
}
componentDidUpdate(){
if((this.state.end - this.state.start) > 5000){
console.log('Function end: ', this.state.end);
clearInterval(this.interval)
}
}
render() {
return (
<div>
<h2>Counter with Function {this.state.number}</h2>
</div>
)
}
}
class Arrow extends React.Component {
constructor(props){
super(props);
this.state = {start: new Date().getTime(),
end: new Date().getTime(),
number: 0};
console.log('Arrow start: ', this.state.start);
this.interval = setInterval(()=>{
let n = this.state.number
this.setState({
...this.state, end: new Date().getTime(), number: ++n
})
}, 1);
}
stop(){
clearInterval(this.interval);
}
componentDidUpdate(){
if((this.state.end - this.state.start) > 5000){
console.log('Arrow end: ', this.state.end);
clearInterval(this.interval)
}
}
render() {
return (
<div>
<h2>Counter with Arrow {this.state.number}</h2>
</div>
)
}
}
class HOC extends React.Component {
render() {
return (<div>
<h1>The one reaching higher count wins</h1>
<Arrow/>
<Fun/>
</div>);
}
}
ReactDOM.render(<HOC />, document.getElementById('react-content'))
Do let me know if your opinion differ