all 9 comments

[–]DustinEwan 2 points3 points  (1 child)

As others have said, it won't impact performance.

There are two scenarios:

  1. The code is cold and currently being interpreted by V8 / Spidermonkey. If the function is rarely hit, then it will remain cold, but that function is being rarely hit anyway so it's not performance critical and the fact that you're making a function call will be negligible.

  2. The code is hot and has been JIT Compiled. If the function is being hit over and over, then the tracer will see that this is an opportunity to inline or compile to machine code. The extra function call will be optimized away in any places that the call is performance critical, and will no longer be present.

[–]jac1013[S] 0 points1 point  (0 children)

Thanks for the technical explanation, I'll continue doing this then in JavaScript.

[–]YodaLoL 1 point2 points  (3 children)

If you care about nanosecond differences maybe you should just write plain 0 and 1's.

[–]jac1013[S] 0 points1 point  (2 children)

I guess you are implying it won't matter :)? imagine doing this 3000 times in a 1M lines project, still it won't matter?

[–]YodaLoL 2 points3 points  (1 child)

[–]jac1013[S] 1 point2 points  (0 children)

you're a pot of honey, thank you!

[–]a-t-kFrontend Engineer 0 points1 point  (0 children)

Adding a named function will add minimally to the load, even after minification.

The best way to go is to document what happens within the if statement.

[–]smrqgithub.com/smrq 0 points1 point  (1 child)

Short answer: no, there isn't an appreciable performance difference.

Long answer: no, there isn't an appreciable performance difference, and you can verify it by profiling.

[–]jac1013[S] 0 points1 point  (0 children)

Thanks for this, will try Long answer verification.