How Do i? Create Dom elemens in JavaScript/jQuery
(table,div,span) using different techniques, their comparison based on performance and memory management.
Part no.1 Introduction
Part no.2 Test no.1
Part no.3 Test no.2
Part no.4 Test no.3,4
Part no.5 Comparison, Result and Conclusion
In many cases we have to create Dom elements (table, div, span, etc) in JavaScript. It could be due to the fact that we have to consume a web service and display data in tabular form. Or update a tabular record by calling an Ajax routine.
There could be many ways to achieve this but the most used form of creating Dom elements are of using the magic strings.
- var div="<div>Operation Completed </div>";
- document.getElementbyId('dvMain').innerHTML=div;
- var element=createElement("div");
- element.innerHTML="Operation Completed";
- var insertBeforeElement=document.getElementById('dvMain');
- document.body.insertBefore(element,insertInside);
- var element=$("<div></div>").text("operation completed");
- $('#dvMain').append(element);
Then there comes the question of performance. If we are not achieving performance or saving memory by the right technique why we are considering this as a better technique? At the end things comes to response time and memory management.
I have created a test pattern to check memory management and responsiveness. I have a JSON object which is an array of object. I will display this object in tabular form by using a generic table creator method and then I will try to extend this method to support more operations like doing an operation when a data is bound to an array.
I will generate about 100 tables to check the responsiveness and then I will use the chrome browser tab management for memory in both cases. Time logging will be done by taking the start time and end time difference.
I have 4 tests in all
1. Creating 100 tables using string
2. Creating 100 tables using jQuery
3. Extending test no.1 when row is bounded with a tr and try to manipulate it
4. Extending test no.2 when row is bounded with a tr and try to manipulate it
JSON object that will be used in every example
- <div class="MsoNormal">var jsonData = [</div><div class="MsoNormal"><span> </span>{ 'sr': 0, 'FirstName': 'Ahmed', 'LastName': 'Mushtaq', 'Mobile': '03003333333', 'isActive': false },</div><div class="MsoNormal"><span> </span>{ 'sr': 1, 'FirstName': 'Ali', 'LastName': 'Raza', 'Mobile': '0300123876', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 2, 'FirstName': 'Ghalia', 'LastName': 'Ahmed', 'Mobile': '0300098765', 'isActive': false },</div><div class="MsoNormal"><span> </span>{ 'sr': 3, 'FirstName': 'Shamoon', 'LastName': 'Perez', 'Mobile': '0300123456', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 4, 'FirstName': 'Chaman', 'LastName': 'Nargis', 'Mobile': '0300987789', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 5, 'FirstName': 'Naseer', 'LastName': 'Ali', 'Mobile': '0300777777', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 6, 'FirstName': 'Sunbal', 'LastName': 'khalid', 'Mobile': '03000987653', 'isActive': false },</div><div class="MsoNormal"><span> </span>{ 'sr': 7, 'FirstName': 'Sameer', 'LastName': 'Raja', 'Mobile': '03002345678', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 8, 'FirstName': 'Salat', 'LastName': 'Khair', 'Mobile': '03008888888', 'isActive': false },</div><div class="MsoNormal"><span> </span>{ 'sr': 9, 'FirstName': 'Shazia', 'LastName': 'Manzoor', 'Mobile': '03003333333', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 10, 'FirstName': 'Shabeer', 'LastName': 'Ali', 'Mobile': '03000987657', 'isActive': false },</div><div class="MsoNormal"><span> </span>{ 'sr': 11, 'FirstName': 'Shoukat', 'LastName': 'Aziz', 'Mobile': '03002345679', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 12, 'FirstName': 'Rizwan', 'LastName': 'Ahmed', 'Mobile': '030000000000', 'isActive': true },</div><div class="MsoNormal"><span> </span>{ 'sr': 13, 'FirstName': 'Jamsheed', 'LastName': 'Junaid', 'Mobile': '03001111111', 'isActive': true}];</div>
It has 14 rows 0 to 13 indexed.
Part no.2
No comments:
Post a Comment