2

I was wondering if there is any difference in performance when dealing with dumb component in React, since there are 2 possible ways to achieve the same result.

function Comp(props) {
    ...
} 

const Comp = props => {
    ...
}
jeremyTob
  • 131
  • 10

2 Answers2

1

Really they are two ways to define a function and there should be no difference in the performances.

pinturic
  • 2,253
  • 1
  • 17
  • 34
-1

There's definitely no difference between the two in your example. Hence this code also gets compiled and you end up having the same thing:

function CompA(props) {}
const CompB = props => {}

gets transpiled to:

function CompA(props) {}
var CompB = function CompB(props) {};

edit: There is difference in both functions tho. In performance they are the same but in behavior the code is different. We have hoisting and different context involved.

edit2: Well, it looks like there IS a difference. Check out https://jsperf.com/react-stateless-compare

Krasimir
  • 13,306
  • 3
  • 40
  • 55
  • There is a difference: Hoisting. `function` gets hoisted, `const` dosn't. – Cerbrus Jan 02 '18 at 13:53
  • Also, arrow functions bind `this` to the surrounding scope - performance-wise, there's probably not any significant difference, but it's incorrect to say that they compile to the exact same code! – Joe Clay Jan 02 '18 at 13:56
  • That is true but the question was about performance. There is no difference in the performance. – Krasimir Jan 02 '18 at 14:01
  • I added a line at the end to clarify. – Krasimir Jan 02 '18 at 14:02
  • There is a difference: https://jsperf.com/react-stateless-compare – Gergely Fehérvári Jan 02 '18 at 14:20
  • 1
    @GergelyFehérvári what about the transpiled version? how does `var CompB = function CompB(props) {};` perform? I guess most people are still transpiling even though chrome doesnt need it! –  Jan 02 '18 at 14:28
  • @GergelyFehérvári that is really weird https://jsperf.com/react-stateless-compare-2 I would love to get an explanation why it is faster. – Krasimir Jan 02 '18 at 15:02
  • This is because the V8 engine implementation. On the other hand, I think we should not care about this performance differences, but good to know, that there is. I think the question should be reopened, because there is a difference, and therefore I can answer properly, as I was about to, but it got closed. – Gergely Fehérvári Jan 03 '18 at 08:55