Are arrow functions faster (more performant, lighter) than ordinary standalone function declaration in v8?

后端 未结 5 1568
太阳男子
太阳男子 2020-12-01 04:42

I am asking this question because I and my colleague have a dispute on coding style because he prefers arrows function declaration:

const sum = (a, b) =>          


        
5条回答
  •  不知归路
    2020-12-01 05:31

    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 (
          

    Counter with Function {this.state.number}

    ) } } 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 (

    Counter with Arrow {this.state.number}

    ) } } class HOC extends React.Component { render() { return (

    The one reaching higher count wins

    ); } } ReactDOM.render(, document.getElementById('react-content'))

    Do let me know if your opinion differ

提交回复
热议问题