Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is wrapping elements so much quicker 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 so much faster creating a new element with jQuery? I would have assumed it was slower due to having to wrap an element with jQuery.

like image 721
diplosaurus Avatar asked Dec 19 '22 12:12

diplosaurus


1 Answers

It takes a long time to parse that string. By giving it an object full of properties, you're doing the bulk of the work for it, resulting in much faster times.

like image 165
Niet the Dark Absol Avatar answered Mar 03 '23 22:03

Niet the Dark Absol