Can anyone may explain me why creating object via string is slower than same object and execute addClass() method in jQuery?
I thought that addClass() method will be slower, but it is not. I'm wondering why?
Look at this jsPerf - http://jsperf.com/jquery-append-with-class-and-with-method-addclass
That's because only passing an element name, like
$("<div>")
, maps to a call to document.createElement().On the other hand, passing an element and its attributes, like
$("<div class='foo'>")
, maps to a call to document.createDocumentFragment(), which is slower thancreateElement()
followed by a write to theclassName
property.