Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

Recently I've been doing a lot of modal window pop-ups and what not, for which I used jQuery. The method that I used to create the new elements on the page has overwhelmingly been along the lines of:

$("<div></div>");

However, I'm getting the feeling that this isn't the best or the most efficient method of doing this. What is the best way to create elements in jQuery from a performance perspective?

This answer has the benchmarks to the suggestions below.

1 Answer

0 votes
by (40.7k points)

You can try using (for readability):

$('<div>');

Use the code given below for safari 3.2.1 / mac os x):

var it = 50000;

var start = new Date().getTime();

for (i = 0; i < it; ++i)  {

  // test creation of an element 

  // see below statements

}

var end = new Date().getTime();

alert( end - start );                

var e = $( document.createElement('div') );  // ~300ms

var e = $('<div>');                          // ~3100ms

var e = $('<div></div>');                    // ~3200ms

var e = $('<div/>');                         // ~3500ms    

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...