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

  • Home
  • SEARCH
  • 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 7398631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:53:38+00:00 2026-05-29T03:53:38+00:00

I have this function that runs over returned json data. It is really fast

  • 0

I have this function that runs over returned json data. It is really fast in chrome, but slow in IE and FF. Suggestions on how I might improve this? The data returned is about 15 objects. This creates a bunch of anchors at the top with lists under each heading.

function list_response(jsonData) {
    "use strict";
    var lists = document.getElementById("lists"), anchors = document.getElementById("anchors"), jItems = jsonData.items;
    jItems.sort(function (a, b) {
            return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
            });
    for (var i = 0; i < jItems.length; i++) {
        var pList = jItems[i], str = "", ank = "";
        str += '<span class="backtotop">[ <a href="#">Back to top</a> ]</span><br /><br /><br /><li class="title nodot"><a class="pHeader" name="' + pList.name + '"><h2>' + pList.name + '</h2></a></li>';
        ank += '<a class="pHeader" href="#' + pList.name + '">' + pList.name + '</a>&nbsp; ';
        lists.innerHTML += str;
        anchors.innerHTML += ank;

        for (var j = 0; j < jsonData.items[i]["videos"].length; j++) {
            var vList = jsonData.items[i]["videos"][j];
            var strs = "";
            strs += '<li class="nodot"><a href="https://www.site.org/videos/Video_Player.page?bctid=' + vList.id + '">' + vList.name + '</a></li>';
            lists.innerHTML += strs;
        }
    }
}
  • 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-29T03:53:39+00:00Added an answer on May 29, 2026 at 3:53 am

    Here’s a version of your code that combines the following performance enhancements:

    1. Only add to innerHTML once at the end of the loop. You want to avoid doing that as often as you can because it causes an entire reparse of all the HTML in that item every time you add onto it. This minimizes the number of transactions with the DOM which can be the slowest part of the whole routine. This improved version cuts the number of DOM transactions a lot. If jItems.length was 20 and the average number of videos was 5, this would cut the number of DOM transactions to 1/50th the number of DOM transactions.
    2. Accumulate string data into an array with .push() and do a .join() at the very end rather than adding onto the end of the string every time. The JS engine can often join lots of strings at once much more efficiently than joining each segment piecemeal.
    3. When accumulating string data into arrays, there is no longer a need to initialize the temporaries on each loop or sub-loop.
    4. Rather than get pList and then have four references to pList.name, just get the name value once and use it directly.
    5. Cache jItems[i] in the loop because it’s referenced several places rather than recalculating it every time.
    6. Calculate the len variable for each for loop just once and compare to that rather than recalculating it on every iteration.
    7. Cache jItems[i]["videos"] once in the outer loop rather than redoing it every time in the inner loop.
    8. If there are a large number of items in jsonData.items, then the sort algorithm isn’t very efficient since it has to recalculate a lowercase version of each name for every comparison. You could prebuild all the lowercase versions in one pass (once per item) and then just use them in the sort algorithm rather than have to redo them every time two items are compared.

    These changes result in this code:

    function list_response(jsonData) {
        "use strict";
        var lists = document.getElementById("lists"), anchors = document.getElementById("anchors"), jItems = jsonData.items;
        var results = [], anks = [], vList, pListName, item, videoItem;
        // precache all lower case versions to make sorting faster
        var i, iLen = jItems.length, j, jLen;
        for (var i = 0; i < iLen; i++) {
            jItems[i].nameLower = jItems[i].name.toLowerCase();
        }
        jItems.sort(function (a, b) {
            return a.nameLower.localeCompare(b.nameLower);
        });
        for (i = 0; i < iLen; i++) {
            item = jItems[i];                   // cache for use in loops
            videoItem = item["videos"];      // cache for use in loops
            pListName = item.name;            // cache for use in multiple places
            results.push('<span class="backtotop">[ <a href="#">Back to top</a> ]</span><br /><br /><br /><li class="title nodot"><a class="pHeader" name="' + pListName + '"><h2>' + pListName + '</h2></a></li>');
            anks.push('<a class="pHeader" href="#' + pListName + '">' + pListName + '</a>&nbsp; ');
    
            for (j = 0, jLen = videoItem.length; j < jLen; j++) {
                vList = videoItem[j];
                results.push('<li class="nodot"><a href="https://www.site.org/videos/Video_Player.page?bctid=' + vList.id + '">' + vList.name + '</a></li>');
            }
        }
        lists.innerHTML += results.join("");
        anchors.innerHTML += anks.join("");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
So I have this function that forks N number of child processes. However it
I have this function called ProperCase that takes a string, then converts the first
I have this function in my Javascript Code that updates html fields with their
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have this function to edit all fields that come from the form and
I have this function, that adds 31px to my style bottom on ul. It
I have this function I'm using and I want to be sure that it
I have this code snippet inside a function that checks if an object exists

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.