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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:59:28+00:00 2026-06-02T18:59:28+00:00

My application has a long list of objects whose width needs to be modified

  • 0

My application has a long list of objects whose width needs to be modified via jQuery. Currently I’m using code like this:

$('#my_list div.text_field').each(function() {
  // Cache objects we're going to use multiple times.
  var $textField = $(this);
  var $listContents = $textField.closest('div.list_contents');

  // Find widths.
  var contentsWidth = $listContents.outerWidth();
  var avatarsWidth = $listContents.find('div.avatars').outerWidth();
  var tagsWidth = $listContents.find('div.tags').outerWidth();
  var textLeft = $textField.position().left;

  // Update the width.
  var newTextWidth = contentsWidth - textLeft - avatarsWidth - tagsWidth;
  $textField.css({ width: newTextWidth });
});

However, it takes a while (> 1 second) when there are hundreds of objects to be manipulated. Any idea how I could make this faster? Should I totally eschew jQuery and use native JS?

  • 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-02T18:59:30+00:00Added an answer on June 2, 2026 at 6:59 pm

    Okay, through a series of improvements I was able to whittle the time it takes to run this code (on Chrome 18 on a series of ~600 items) down from over 3000 ms to 70ms.

    The most drastic improvement came from using offsetWidth on the raw HTML elements instead of jQuery’s outerWidth() statement. That alone shaved off over 50% of the time:

    avatarsWidth = $listContents.find('div.avatars')[0].offsetWidth;
    

    The second most drastic change came from reducing the number of DOM modifications I made. In the code above, I was looping through elements, calculating their widths, and then immediately applying these widths to the DOM. In my improved code, I still loop through to calculate the widths, however I then store those widths, detach the elements from the DOM, apply the stored widths, and reattach them. Thanks to @muffel for this idea. This shaved off over 30% of the total time:

    $('#my_list div.text_field').each(function() {
      var $textField = $(this);
      // ...
      var newTextWidth = contentsWidth - textLeft - avatarsWidth - tagsWidth;
      $textField.attr('data-width', newTextWidth);
    });
    
    $('#my_list')
      .detach()
      .find('div.text_field')
        .each(function() {
          $(this).css({ width: $(this).attr('data-width') });
        })
        .end()
      .appendTo('#container');
    

    The third biggest improvement came from reducing the number of times I traversed the DOM. Instead of selecting elements each time through the loop, I selected them all up front and then referenced the indices inside the loop. This made up the majority of the remaining improvement:

    var $avatars = $('#my_list .avatars');
    // ...
    $('#my_list div.text_field').each(function(i) {
      // ...
      avatarsWidth = $avatars.eq(i).offsetWidth;
      // ...
    });
    

    Hope this helps someone!

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

Sidebar

Related Questions

In my application has a ListView. When long press on item the Context Menu
My application has encountered a problem and needs to close. Of course Microsoft is
I have an application with a long list that changes frequently, and I need
I have a server application that has been instrumented using JMX so that it
I have an application that has several objects (about 50 so far, but growing).
This is my first iPhone application. So I have this long list of doubts
I have an application that has 3 level of data depth: list of presentations
i'm building small sms-like application. I made conversation list view, but there is a
My application has a UITableViewController which has a list of cells representing names of
Our application has a service layer and a DAO layer, written as Spring beans.

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.