JavaScript How can I optimize the jquery script to improve performance? optimize javascript jquery

I have this JQuery script that is looking at some DIVs and getting the image sizes and setting the div size according to the images.

I had a JQuery script to get all the DIVs and loop to each image and get the size and set the DIV size according to the correct size. The problem was that the script was running strange, it only worked when I refresh(F5) the page. Now if I change to get all the images first and loop to get the size it works fine. The only issue with the second option is that I don't know what images belong to what DIVs, so I had to add an extra loop to get all the images from the correct DIV.

Here is my code:

$('.groupContainer').imagesLoaded(function($images){
        $(this).each(function(){
            var rowWidth = 16;
            $(this).imagesLoaded(function($images){
                $images.filter(':not(".overlayBoxImage")').each(function(){
                    if($(this).context.id != 'groupTitle'){
                        $(this).parent().parent().parent().css('width', $(this).width());
                    }
                    rowWidth += $(this).width();
                });
                if(rowWidth > maxWidth){
                    maxWidth = rowWidth;
                    $('.vnBreadcrumbsCont').css({width: maxWidth, 'max-width':maxWidth});
                    $('.guideDescCont').css({width: maxWidth, 'max-width':maxWidth});
                    $('#headerContainer').css({width: maxWidth});
                    $('.groupContainer').css({width: maxWidth,'max-width': maxWidth});
                    $('#guideContainer').css({width: maxWidth, 'max-width': maxWidth});
                }
            });
        });
    });

I would like some help to see if I can optimize the LOOPs. Because current I'm looping to get the image (don't do anything with it), and looping again to get all the DIV and looping again to get all the images and doing the calculation.

This is the script I had before, but it does not work proper because I load the DIVs first and loop the images.

$('.groupContainer').each(function(){
        var rowWidth = 16;
        $(this).imagesLoaded(function($images){
            $images.filter(':not(".overlayBoxImage")').each(function(){
                if($(this).context.id != 'groupTitle'){
                    $(this).parent().parent().parent().css('width', $(this).width());
                }
                rowWidth += $(this).width();
            });

            if(rowWidth > maxWidth){
                maxWidth = rowWidth;
                $('.vnBreadcrumbsCont').css({width: maxWidth, 'max-width':maxWidth});
                $('.guideDescCont').css({width: maxWidth, 'max-width':maxWidth});
                $('#headerContainer').css({width: maxWidth});
                $('.groupContainer').css({width: maxWidth,'max-width': maxWidth});
                $('#guideContainer').css({width: maxWidth, 'max-width': maxWidth});
            }
        });
    });
Answer:1

I am using: Grunt with the watch plugin for live reload & Sublime Text 2's SFTP plugin for uploading files on save Live reload is just a little quicker than the SFTP upload. Using Guard, you ...

I am using: Grunt with the watch plugin for live reload & Sublime Text 2's SFTP plugin for uploading files on save Live reload is just a little quicker than the SFTP upload. Using Guard, you ...

  1. grunt livereload not working
  2. grunt livereload example
  3. grunt livereload port
  4. grunt livereload
  5. grunt livereload change port
  6. grunt livereload browser
  7. grunt livereload options
  8. grunt livereload chrome
  9. grunt livereload watch
  10. grunt livereload not reloading
  11. grunt livereload middleware
  12. grunt watch live reload
  13. magento 2 grunt livereload
  14. grunt connect livereload
  15. grunt connect livereload not working
  16. grunt watch livereload example
  17. grunt-contrib-connect livereload
  18. grunt server livereload
  19. grunt watch livereload not working
  20. grunt connect livereload example

I have a bootstrap modal contact form which uses AJAX and PHP to save the information sent by a user to a database: <div class="modal fade" id="contact" role="dialogue"> <div ...

I have a bootstrap modal contact form which uses AJAX and PHP to save the information sent by a user to a database: <div class="modal fade" id="contact" role="dialogue"> <div ...

I learnt to write angular dependencies needed using the array notation, that way: var app = angular.module('MyApp', []); app.controller('MyCtrl', ['$scope', function($scope) { $scope.stuff = '...

I learnt to write angular dependencies needed using the array notation, that way: var app = angular.module('MyApp', []); app.controller('MyCtrl', ['$scope', function($scope) { $scope.stuff = '...

I have a controller with 2 scopes: app.controller('search', function($scope) { $scope.value1 = ''; $scope.value2 = 'Some text' + $scope.value1; } And an input field: <input type="text" ...

I have a controller with 2 scopes: app.controller('search', function($scope) { $scope.value1 = ''; $scope.value2 = 'Some text' + $scope.value1; } And an input field: <input type="text" ...

  1. angular update scope
  2. angular update scope variable
  3. angular update scope after ajax
  4. angular update scope value
  5. update angular scope from jquery
  6. angular refresh scope
  7. angular change scope variable
  8. angular refresh scope value
  9. angular change scope variable in html
  10. angular refresh scope variable
  11. angular change scope
  12. angular change scope variable in directive
  13. angular change scope outside controller
  14. angular change scope from outside
  15. angular change scope variable value
  16. angular change scope value
  17. angular 1 update scope
  18. angular update parent scope from child
  19. angularjs update scope
  20. angular force update scope variable