Why element aggregation is much faster with jQuery elements

Check out the test: http://jsperf.com/wrap-with-jq

var s = $('<span />').text('my span');



s.wrap('<div id="myWrap" class="myClass"></div>').parent();

8 073 ops / sec

87% slower

s.wrap($('<div />', {
  'id': 'myWrap',
  'class': 'myClass'
}).parent());

72,955 ops / sec

Is there a reason this is much faster when creating a new element with jQuery? I would suggest that this was slower due to the need to wrap an element using jQuery.

+4
source share
3 answers

In fact, this is not so much. There is a typo in your test ...

s.wrap($('<div />', {
  'id': 'myWrap',
  'class': 'myClass'
// move `parent` call outside...
})).parent();

, , parent, ([]), s.wrap, s , dom, .

http://jsperf.com/wrap-with-jq/3

5,118 4,149 / jq

+1

. , , , .

+6

, jQuery, JS-, , div, , documentFragment, HTML-, . , , HTML.

: . , HTML , , , , .

, , , .

+3
source

All Articles