Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 735643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:30:50+00:00 2026-05-14T07:30:50+00:00

Ok, I’m rewriting some vanilla JS functions in my current project, and I’m at

  • 0

Ok, I’m rewriting some vanilla JS functions in my current project, and I’m at a point where there’s a lot of HTML being generated for tooltips etc.

My question is, is it better/preferred to do this:

var html = '<div><span>Some More Stuff</span></div>';
if (someCondition) {
    html += '<div>Some Conditional Content</div>';
}
$('#parent').append(html);

OR

var html = $('<div/>').append($('<span/>').append('Some More Stuff'));
if (someCondition) {
    html.append($('<div/>').append('Some conditionalContent');
}
$('#parent').append(html);

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:30:50+00:00Added an answer on May 14, 2026 at 7:30 am

    From a performance standpoint: it depends.

    In your short example, it’s faster to append the text, since you actually aren’t creating any DOM elements until the end. However if you were doing this a lot, then the added time of string concatenation vs the performance of cached document fragments adds up.

    When you do $(html) jQuery caches it as a document fragment (provided the string is 512 bytes or less), though there’s not much gain if you’re caching just $("<div />")…however if you’re doing it thousands of times, there is a measurable impact, as string concatenation gets more expensive as your string gets longer, the cached document fragment cost is pretty steady.

    Update: Here’s some quick examples to see what I mean, use firebug to get the console times here:

    You can run this for yourself: http://jsfiddle.net/Ps5ja/

    console.time('concat');
    var html = "";
    for(var i = 0; i < 500; i++) {
        html += '<div><span>Some More Stuff</span></div>';
        html += '<div>Some Conditional Content</div>';
    }
    var elem = $(html);
    console.timeEnd('concat'); //25ms
    
    console.time('DOM');
    var parent = $("<div />")
    for(var j = 0; j < 500; j++) {
        parent.append($('<div/>').append($('<span/>', {text :'Some More Stuff'})));
        parent.append($('<div/>',{ text: 'Some conditionalContent' }));
    }
    console.timeEnd('DOM'); //149ms
    
    console.time('concat caching');
    var html = "";
    for(var i = 0; i < 5000; i++)
        html += '<div><span>Some More Stuff</span></div><div>Some Conditional Content</div>';
    var elem = $(html);
    console.timeEnd('concat caching'); //282ms
    
    console.time('DOM caching');
    var parent = $("<div />")
    for(var j = 0; j < 5000; j++)
        parent.append('<div><span>Some More Stuff</span></div><div>Some Conditional Content</div>');
    console.timeEnd('DOM caching'); //157ms
    

    Note: the var elem = $(html); in the string test is so we end up creating the same DOM elements, otherwise you’re comparing string concatenation to actual DOM creation, hardly a fair comparison, and not really useful either 🙂

    You can see by the above, as the cached fragment is more complex, the more caching makes an impact. In the first test, which is your example without the condition cleaned up a bit, DOM loses because there are lots of little operations going on, in this test (on my machine, but your ratios should be about the same): HTML Contact: 25ms, DOM Manipulation: 149ms.

    However, if you can cache the complex fragment, you get the benefit of not creating those DOM elements repeatedly, just cloning them. In the second test DOM wins out, because while the HTML method creates that DOM element collection 5000 times, the second cached method only creates it once, and clones it 5000 times. In this test: HTML Concat: 282ms, DOM Manipulation: 157ms.

    I realize this isn’t directly in response to your question, but based on comments it seems there’s some curiosity about performance, so just giving something you can see/test/play with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.