Skip to content

24 ways to impress your friends

Vote up?

JulienW

I also strongly disagree with the last part. Not about its speed but about its security. Really, doing string concatenation should light a warning in your head ! And I keep seeing these advice on every website talking about jQuery !

Really, what is faster here is that you don’t access the node at each step of the loop. You can do this by adding elements to an “off-DOM” node. Yes, it will probably be slower than string concatenation, but still way faster than using an attached DOM node.

So instead of using string concatenation, use jQuery’s “attr” function on a unattached DOM, and it should be nearly as fast.

jsperf testcase to demonstrate this

(feel free to fiddle with the testcase as it doesn’t seem so reliable on my firefox right now)