Skip to content

"Clever Conditionals" in Performance is wrong #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ithcy opened this issue Feb 22, 2013 · 8 comments
Closed

"Clever Conditionals" in Performance is wrong #258

ithcy opened this issue Feb 22, 2013 · 8 comments

Comments

@ithcy
Copy link

ithcy commented Feb 22, 2013

http://learn.jquery.com/performance/clever-conditionals/

jsperf: http://jsperf.com/jquery-clever-conditionals

The "better" way (using a regex) is 94% slower than the "old way" (simple equality comparison), and I would argue that it's also much less obviously understandable when skimming over code.

The object literal lookup method is also 78% slower than the "old way", but it's not implied that it's "better". I'm not sure what the point is of this example as it's almost as slow and hard to read as the "better" way.

Also, the "old way" should probably use the triple-equals for comparison.

@rwaldron
Copy link
Member

@ithcy Can you make a pull request that updates the content with correct information?

Include:

  1. The explicit condition is clearer to a future reader/maintainer of the code.
  2. Performance benefit
  3. ...Anything else relevant?

@ithcy
Copy link
Author

ithcy commented Feb 22, 2013

@rwldrn I could, but honestly I'm not sure this page should even exist... It looks like it was pulled wholesale onto learn.jquery.com along with the rest of the Performance Best Practices section of jQuery Fundamentals, who in turn got it from Paul Irish's 2009 "jQuery Anti-Patterns for Performance" presentation (http://paulirish.com/2009/perf/ , 48:20 in the video or slide 61).

It just gets a brief mention in the presentation, and only in the specific context of saving a few bytes when you're forced to include some JS in the document head rather than at the end of the body. The 2 latter examples are there because they take up proportionally fewer bytes as the number of comparisons grows. So they're better for page-loading performance, worse for overall computational performance, but people are going to assume that example 2 and 3 are just "best practice because jquery.com says so" and use them all over without understanding what they're doing.

In other words if this was a question on stackoverflow I'd vote to close it as "too localized" :) I'm not sure it belongs at all in the performance section of a site that's sure to be the first stop for thousands of new jQuery students, especially when it's just sort of blobbed in there without the lengthy discussion that's needed to establish the correct context for this very specific bit of advice.

Do you think the page should be amended to add the context. or just removed?

@ajpiano
Copy link
Member

ajpiano commented Feb 22, 2013

I think we should just remove it for now

@ithcy
Copy link
Author

ithcy commented Feb 22, 2013

@ajpiano You need to remove the reference from order.yml as well.

@ajpiano
Copy link
Member

ajpiano commented Feb 22, 2013

I just did

@ajpiano
Copy link
Member

ajpiano commented Feb 22, 2013

GMTA :) Thanks for noticing this @ithcy

@ithcy
Copy link
Author

ithcy commented Feb 22, 2013

NP! :)

teamunity added a commit to ithcy/learn.jquery.com that referenced this issue Feb 22, 2013
@rwaldron
Copy link
Member

@ithcy +1 (even though it already happened) I wanted to encourage you to contribute to the overall improvement of the content.. which you did, but by other means :D

Thanks!!

arthurvr pushed a commit to arthurvr/learn.jquery.com that referenced this issue Jan 4, 2015
…tion is too context specific and not a generally applicable best practice. Fixes jquery#258.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants