react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

Update tutorial.md

Open ch-ricardor opened this issue 6 years ago • 3 comments

This change is irrelevant for the tutorial but could be useful to improve performance in complicated conditions and calculations.

I confirmed a behavior, in an IF-THEN-ELSE with OR conditions, the "if" will test the state of the sentences to get a TRUE and omit the other clauses. (Considering this could improve performance and reduce processor cycles in more complex calculations)

Could be added a recommendation: - If the result of a function will be tested more than once, run it once and store the result in a variable, considering the scope of that variable.

In this case:

if ( squares[i] || calculateWinner(squares)) {

Test square first, if it has a move, won't test the "calculateWinner".

In this case the "for" has few cycles,

image

If you test:

if ( calculateWinner(squares) || squares[i]) {

With this additional cycle in calculateWinner function will notice the difference if the user clicks on a played square.

  for (let i = 0; i < 10000000000; i++) {
  }

image

or adding an:

alert('testing winner');

Nice explanation in how to build and refactor classes.

ch-ricardor avatar Mar 26 '19 09:03 ch-ricardor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

facebook-github-bot avatar Mar 26 '19 09:03 facebook-github-bot

Deploy preview for reactjs ready!

Built with commit b416c6068dcc6d0da4e8417d68db1ce3dd8f0d0e

https://deploy-preview-1871--reactjs.netlify.com

reactjs-bot avatar Mar 26 '19 09:03 reactjs-bot

In this case the readability stays the same (and can even be improved by putting squares[i] into some const) while performance is indeed improved a little. @ch-ricardor To have it merged though this PR needs to be rebased as this tutorial was moved to other file.

BartoszKlonowski avatar Jun 16 '24 13:06 BartoszKlonowski