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 7959779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:36+00:00 2026-06-04T04:37:36+00:00

I have a JavaScript Object with some information in it. I have 2 options

  • 0

I have a JavaScript Object with some information in it.

I have 2 options I can think of to create the HTML from this object. I was wondering which one is the correct way of doing things of is this just all preference?

1) Loop through this array with JavaScript and create the HTML with Jquery?

2) Ajax post/get the object to PHP and loop through this object(php array) and create the HMTL that way? Then return a json encoded object back with the HMTL in it and append it to a div?

What I currently Do to build

    var OutterDiv = $(document.createElement('div'));

    for loop{
        OutterDiv.append("<span>SOME INFO</span>");


        var InnerDiv = $(document.createElement('div'));
        for loop{
            InnerDiv.append("<span>SOME INFO</span>");
            InnerDiv.append("<span>SOME INFO</span>");
        }

        OutterDiv.append(InnerDiv);
    }


    $("#content").append(OutterDiv);
  • 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-06-04T04:37:37+00:00Added an answer on June 4, 2026 at 4:37 am

    Why don’t you loop through the object and create an HTML string from JavaScript? Then insert that string wherever you need it? I believe this is the fastest way you can accomplish what you want do do. The main idea is that concatenating strings is faster than inserting DOM elements, and perhaps faster than the latency caused by an Http request.

    ** Edit **

    Apparantly, IE is slower at string concatenation (big surprise) and using an array is better.
    Example :

    var html = [];
    for (...) {
       html.push( <some html content from your object here> );
    }
    $(selector).html(html.join(''));
    
    // find the elements you need to handle and perform bindings here
    // ex: $('#someelment').click(...);
    

    This is probably as fast as you can get.

    ** Update **

    While performing the task of building HTML with JavaScript is still generally faster, after some testing, it seems that concatenating strings, or building arrays are not faster than creating text nodes. The test can be viewed and forked on jsfiddle.net or here it is for archiving pruposes :

    function runTest(testFn, duration) {
    
        var endTime = +new Date() + duration;
        var runs = 0;
        var charCount = 0;
    
        while (+new Date() < endTime) {
            charCount += testFn();
            ++runs;
        }        
        teardown();
    
        //console.log(testFn.title, 'ran', runs, 'times.');
        $('#log').append($('<div></div>').text(testFn.title + ' ran ' + runs + ' times (' + (charCount/1000) + ' cps).'));
    }
    
    ///
    
    function teardown() {
        while (targetDiv.firstChild) {
            targetDiv.removeChild(targetDiv.firstChild);
        }
    }
    
    ///
    
    var testData;
    var sample, sampleData;
    function generateTestData() {
        testData = {};
        for (var i=0; i < (Math.random() * (sample[1]-sample[0])) + sample[0]; i++) {
            testData['item'+i] = randomString();
        }
    }
    
    
    function randomString()
    {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789";
    
        for( var i=0; i < (Math.random() * (sampleData[1]-sampleData[0])) + sampleData[0]; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));
    
        return text;
    }
    
    function shuffle(arr) {
        var len = arr.length;
        var i = len;
        while (i--) {
            var p = parseInt(Math.random()*len);
            var t = arr[i];
            arr[i] = arr[p];
            arr[p] = t;
        }
        return arr;
    };
    
    ///
    
    var $targetDiv = $('#targetDiv');
    var targetDiv = document.getElementById('targetDiv');
    
    ///
    
    function jq() {
    
        var html = [];
        var count = 0;
    
        for (var key in testData) {
            count += testData[key].length;
            html.push('<div>' + testData[key] + '</div>');
        }
    
        $targetDiv.html(html.join(''));
    
        return count;
    }
    
    function inner() {
    
        var html = [];
        var count = 0;
    
        for (var key in testData) {
            count += testData[key].length;
            html.push('<div>' + testData[key] + '</div>');
        }
    
        targetDiv.innerHTML = html.join('');
    
        return count;
    }
    
    
    function dom() {
    
        var root = document.createElement('div');
        var node;
        var count = 0;
    
        for (var key in testData) {
            count += testData[key].length;
            node = document.createElement('div');
            node.appendChild(document.createTextNode(testData[key]));
            root.appendChild(node);
        }
        targetDiv.appendChild(root);
    
        return count;            
    }
    
    ///
    
    jq.title = 'jQuery .html';
    inner.title = 'innerHTML';
    dom.title = 'DOM';
    
    ///
    
    sample = [10, 100];
    sampleData = [100, 1000];
    generateTestData();
    
    var duration = 1000;
    var testFn = shuffle([jq, inner, dom]);
    
    $.each(testFn, function(k, fn) {
        setTimeout(function() { runTest(fn, duration); }, 0);
    });
    ​
    

    Overall, while each method is more efficient under some conditions (lots of or few data, long or short strings, etc.), the DOM method seems generally faster in all cases.

    So, there you have it. Thanks to GGG for the initial test case.

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

Sidebar

Related Questions

I have a JavaScript object like this one, which is responsible for managing messages:
i have a object in javascript and some already defined functions. but how can
In JavaScript, if I have some function I can use the arguments object to
Suppose I have a javascript object like this: window.config config.UI = { opacity: {
I have a GridView with some information in it. When I click on one
Assume I have a RESTful web service, which holds information about an object that
Just wonder. I have extended javascript Array object using its prototype as follows: <html>
I have a JavaScript object that I'd like to add some properties to, but
I have some modular ( object and classes ) JavaScript and PHP. I keep
I am mlearning javascript and have some trouble creating an onject via prototype. I

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.