Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

AurelioDeRosa
Copy link
Member

Updated the last example adopting a for loop and length cache. We should recommend how to achieve the best performance.

Updated the last example adopting a for loop and length cache. We should recommend how to achieve the best performance.
@mgol
Copy link
Member

mgol commented Dec 4, 2013

Caching the array length isn't really important in current browsers.

@AurelioDeRosa
Copy link
Member Author

@mzgol Basically the PR was made to change the $.each with the for. Then I included the caching because it's currently included in the doc.

@@ -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.
Copy link
Member

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.

Copy link
Member Author

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.

@scottgonzalez
Copy link
Member

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 $.each() is pretty low, especially compared to DOM modifications.

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.

@AurelioDeRosa
Copy link
Member Author

@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.

@scottgonzalez
Copy link
Member

It's more than one line. It's a complete change in the style. You've lost item and now need to use array references, and variables declared inside the loop don't have their own scope anymore.

@tjvantoll
Copy link
Member

I agree with @scottgonzalez. The point of this article is to avoid DOM interactions and this moves the focus away from that.

@ajpiano
Copy link
Member

ajpiano commented Dec 4, 2013

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

@AurelioDeRosa AurelioDeRosa deleted the patch-2 branch July 7, 2014 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants