-
Notifications
You must be signed in to change notification settings - Fork 481
Update append-outside-loop.md #445
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
Conversation
Updated the last example adopting a for loop and length cache. We should recommend how to achieve the best performance.
Caching the array length isn't really important in current browsers. |
@mzgol Basically the PR was made to change the |
@@ -37,16 +37,17 @@ $.each( myArray, function( i, item ) { | |||
$( "#ballers" )[ 0 ].appendChild( frag ); | |||
``` | |||
|
|||
Another simple technique is to build up a string during each iteration of the loop. After the loop, just set the HTML of the DOM element to that string. | |||
Another simple technique is to build up a string during each iteration of the loop. To have better performances, the iteration should use a straight `for` loop with [cached length](http://learn.jquery.com/performance/cache-loop-length/). After the loop, just set the HTML of the DOM element to that string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"performances" should be "performance" (singular). A couple other tweaks would be helpful, too:
For better performance, the iteration could use ...
I like "could" here, because $.each()
is not always a performance problem. If you're only iterating over handful of items (and not doing so thousands of times), it's not going to be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kswedberg I agree on the use of could instead of should. Updated using your suggestions.
I'd much rather see the example stay as-is and just add a sentence about switching to for and linking to the loop caching article at the end. The overhead of The reader should be focusing on the purpose of the article, which is reducing the number of times the DOM is touched. We shouldn't distract them with for loops and length caching. Especially since it changes scoping. |
@scottgonzalez A change in a loop statement doesn't seem much distracting to me (1 line) but add value to the information. Anyway, the decision of merge it or not, it's up to you. |
It's more than one line. It's a complete change in the style. You've lost |
I agree with @scottgonzalez. The point of this article is to avoid DOM interactions and this moves the focus away from that. |
I'm with @scottgonzalez and @tjvantoll on this particular issue -- the point here is to cut down on reflows, not improve performance by changing the loop style -- pehaps we should mention that specifically |
Updated the last example adopting a for loop and length cache. We should recommend how to achieve the best performance.