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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:14:16+00:00 2026-05-16T11:14:16+00:00

The following function needs at least 3 seconds to run (on 500 table rows).

  • 0

The following function needs at least 3 seconds to run (on 500 table rows). Is it possible to make this function faster?

function prepareTable() {
    var groupIndex = 0;
    $("#row tbody tr").each(function(index) {
    // each row gets a unique id
    // remove default css styles for table rows
    // read out hidden value, that stores if row is a group
    var group = $(this).attr('id', 'node-'+index).removeClass("odd event").find('td :hidden').attr('value');
    // if it is a group, add special styles to row and remember row index
    if (group == 'true') {
        groupIndex = index;
        $(this).addClass('odd').find("td:first")
            .mouseenter(function() {
                $(this).parent().addClass("swGroupLink");
            })
            .mouseleave(function() {
                $(this).parent().removeClass("swGroupLink");
        });
    } else {
        // make all following rows to children of the previous group found
        $(this).addClass('even child-of-node-' + groupIndex);
    }   
    });
}
  • 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-16T11:14:17+00:00Added an answer on May 16, 2026 at 11:14 am

    I suggest two improvements:

    • Cache DOM References
    • Work at your table offline

    Example

    function prepareTable() {
       var groupIndex = 0;
       var $mytable = $('#row'),
           $parent  = $mytable.parent();
    
       $mytable = $mytable.detach();
    
       $mytable.find('tr').each(function(index) {
          var $this = $(this);
          var group = $this.attr('id', 'node-'+index).removeClass("odd event").find('td :hidden').attr('value');
    // if it is a group, add special styles to row and remember row index
       if (group == 'true') {
           groupIndex = index;
           $this.addClass('odd').find("td:first")
               .mouseenter(function() {
                   $this.parent().addClass("swGroupLink");
               })
               .mouseleave(function() {
                   $this.parent().removeClass("swGroupLink");
           });
       } else {
           // make all following rows to children of the previous group found
           $this.addClass('even child-of-node-' + groupIndex);
       }   
       });
    
       $parent.append($mytable);
    }
    

    I added a variable $this which caches $(this) in your .each() loop. I also added $mytable and $parent. $mytable stores the #row element and $parent stores the parent-node from #row. That is because I remove the whole element from the DOM, do the work and re-attach it to the parent.

    Test environment: http://www.jsfiddle.net/2C6fB/4/

    If that is still too slow, you have other options here. First, look if you can split the loop into smaller pieces. You can optimize that like a lot by using asychronous callbacks, for instance, setTimeout. That can be a tricky business and I would need to know your code in more detail, but in general you might just want to wrap your whole loop into single setTimeout() functions. Example -> http://www.jsfiddle.net/2C6fB/5/

    This ensures that the browser won’t “hang” while operating. But of course this took a little bit longer to complete the whole task.

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

Sidebar

Related Questions

can someone help with this? I need the following function to do this... $x
I need to call a function in the following fashion: var f = 'fadeOut';
The following example needs to be running in IE 9 and in at least
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need to sort string, and I came up with the following function. def
I need to check if an object contains any value. The following function works
I need the following C function in Python: int func(Uint8 *bytRecvBuffer, int *iRecvLen); I
Morning- I need a function that would produce the following kind of looking sequence:
I need a function with the following signature: System.Reflection.PropertyInfo getPropertyInfo(System.Type type, string NavigationPath) or
I need to add fade function in the following script. I need that the

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.