On Dec 22, 7:11 am, Andraž Kos <[email protected]> wrote: > Is in your opinion better to create a DOM tree only once, clone it and then > traverse the clone only to update data, or to create everything by hand over > and over again?
It is generally faster to clone a structure than repeatedly create it, if for no other reason than you can clone it with a single call whereas building a complex structure may require several (or dozens) of calls. However, if you clone a structure, you generally then have to traverse it and fix duplicate ids and content, which would have been done along the way if you'd created the structure. The difference in performance may be negligible after *all* processing is considered, don't focus on just the creation part. A mix of the two may be best (where "best" is not just what is fastest). Performance also varies by browser - at one time, DOM methods in Safari were faster than innerHTML. So do whatever suits based on the best logical structure and if there's a performance issue, address just those parts that need it. > Example jsFiddle:http://jsfiddle.net/77Zxy/ Your choice of library may well be adding more overhead than the difference between the two approaches. Creating 100+ identical elements is not a common requirement. Testing using unrealistic scenarios will return less useful results, so choose something that closely resembles what you want to do in production (e.g. include processing to modify the content of each cell and perhaps to modify element properties such as id and class) . -- Rob -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
