Matthew
Matthew

Reputation: 11270

Wrap huge amount of HTML by jQuery's $(). What affects performance?

I'm wrapping a huge amount of HTML by $() It takes about 250 ms. What affects the performance: the length of the html string, or the number of elements in the html string ?

Upvotes: 1

Views: 389

Answers (1)

Mathias Bynens
Mathias Bynens

Reputation: 149564

Both, but the number of elements has a much bigger impact than the string length.

The longer the string, the more that will be needed to parse it.

The higher the number of elements that need to be created, the longer it will take.

If you’re really curious, why not create a jsPerf test case? In the first test, you could have a single paragraph with a lot of text content, and in a second test you could place multiple <p> elements with no content. Make sure both strings have the same length.

Update: I’ve created an example jsPerf test case that demonstrates that the number of elements is indeed much more important than the string length. http://jsperf.com/jquery-htmlstring

Upvotes: 2

Related Questions