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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With