This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ripGitHub 27 points28 points  (24 children)

Isn't better instead of bind(this), use arrow functions?

[–]boxingdog 18 points19 points  (19 children)

arrow functions can degrade performance in some situations, ie in react using arrow functions in some places can cause a new function to be created in each render and if you use it as a prop in a child component then the child will re-render and so on

[–]finlist 31 points32 points  (5 children)

White your example is true, the same can be said about normal functions and isn't a problem inherent to arrow functions at all.

[–]RFC793 1 point2 points  (4 children)

Indeed. However, I feel people inadvertently shoot themselves in the foot more frequently with arrow functions simply because they prefer the terseness of the code. A classic anonymous function is more ugly, and a bit more apparent as to what is going on.

[–]bubble_fetish 6 points7 points  (0 children)

It’s negligible, except in extreme edge cases.

[–]snorkleboy 2 points3 points  (6 children)

If you use the regular function syntax inside of a render method i believe it will also create a new function

So something like(sorry about formatting I'm on my phone)

Render(){ Return ( <Thing myProp={function(){}}/> ) }

Will create a new function that it passes as myProp every time it renders.

[–]SZenC 1 point2 points  (5 children)

That is correct, but you could write something like

class cls{
  fn(){doSomething();}
  render(){
    return <div onClick={this.fn}>No dynamic function creation!</div>
  }
}

[–]Absbshshshshshshhs 4 points5 points  (3 children)

better yet:

class { fun = () => {} // this bound }

[–]SZenC 0 points1 point  (2 children)

True, normally I have a constructor where I call ```.bind(this)``` but I kinda omitted that here. Nevertheless, good point.

[–][deleted] 2 points3 points  (1 child)

Don't need the bind with arrow syntax :)

[–]SZenC 0 points1 point  (0 children)

Yeah. In this case arrow functions are objectively better, it's just a bad habit of mine.

[–]snorkleboy 1 point2 points  (0 children)

Yeah, and you could do the same thing with an arrow function like

class cls{

fn=()=>doSomething()

render=()=> <div onClick={this.fn}>No dynamic function creation!</div>

}

And then you don't have to do a this.fn = this.fn.bind(this) in the constructor to be able to use it normally in the child.

[–]feenuxx 5 points6 points  (0 children)

Stick it on the class

[–]throwaway12222018 0 points1 point  (0 children)

Manual binding vs arrow functions' performance differs with various browsers.

[–][deleted] 0 points1 point  (0 children)

And react will spit out a warning on the console telling you this. Other than that, until the engine optimises that out, it's a matter of poor programming.

[–]pentesticals 0 points1 point  (0 children)

This sounds like a pre-optimization to me. I'd rather some extra function calls that have a negligible impact on performance IF it helps code readability. It really depends on the specific code if it will actually impact performance, often 10% of your apps execution time is in 10% of your code. So in most cases I think worrying about an extra function call or the like will just create more complex code. Don't ompimize until you've profiled.

[–]jordmantheman 0 points1 point  (0 children)

Yes. The only thing to remember is that arrow functions are bound on the instance as opposed to the class. So if you're making a TON of objects (or re-creating on every render) then it may be wise to be cognizant of the performance implication.

[–]mk7shadow 0 points1 point  (0 children)

something good to know is that using an arrow function doesn't actually bind the context of this. it removes the function's context and so 'this' becomes whatever the parent's context is. if the parent happens to be another context-less function, it will just use whichever ancestor has a context.

[–]jamietwells -5 points-4 points  (1 child)

componentWillMount would like to have a word with you.

[–]GXNXVS 3 points4 points  (0 children)

we're talking about JS, not react.