One of jQuery's greatest strengths is its ability to manipulate the DOM with a minimal amount of code. It's so easy to change things that we often don't think about what's happening under the covers. For example, consider the following code that appends nodes to an object named parentDiv.
var parentDiv = $('#emailList'); for (var i = 0; i < 100; i++) { parentDiv.append('<div>' + i + '</div>'); }
This is one of the more common ways to append nodes into a given parent. Although it works fine, it's not optimal because jQuery has to constantly perform DOM operations in the loop. You may not notice a problem when looping through 100 items, but as that number increases to a larger number the performance can start to degrade. A more efficient (yet simple) approach from a performance standpoint is shown next:
var parentDiv = $('#emailList'); var divs = ''; for (var i = 0; i < 100; i++) { divs += '<div>' + i + '</div>'; } parentDiv.html(divs);
The previous code only touches the DOM once after the loop has completed resulting in better overall performance. While a lot of string concatenation is occurring, it turns out that going that route is much more efficient than calling append() multiple times.
Keep in mind that you can always manipulate the DOM directly as well and increase performance even more in some cases by using things like document.createDocumentFragment() and other techniques. While I prefer to use jQuery functions whenever possible, sometimes it's necessary to step outside of jQuery’s API for a given task and use the native DOM API instead.
A test page available at http://jsperf.com/strings-vs-array-join-vs-doc-fragment/8 shows different techniques that can be used for updating the DOM while using a loop as well as their individual performance results. An example of the test results and the different techniques tested are shown next. You’ll notice that calling jQuery’s append() function in a loop doesn’t fare too well compared to some of the other more “native” DOM options.
Test | Ops/sec | |
---|---|---|
Strings
|
| 1,226±0.84%61% slower |
Array (using push)
|
| 1,234±0.35%61% slower |
Doc Fragment
|
| 3,020±3.24%fastest |
Array (using index)
|
| 1,223±0.46%61% slower |
Array join then string concat
|
| 1,252±0.37%60% slower |
DOM List
|
| 3,144±0.00%fastest |
jquery
|
| 428±9.06%86% slower |
doc fragment 2
|
|
Conclusion


If you’re interested in learning more about jQuery or JavaScript check out my jQuery Fundamentals or Structuring JavaScript Code courses from Pluralsight. We also offer onsite/online training options as well at http://www.thewahlingroup.com.
No comments:
Post a Comment