Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large Isotope Gallery is Very Slow

I have an Isotope gallery (version 2) with almost 400 elements. A typical gallery item looks like this:

<div class="element hybrid a" data-category="hybrid">
    <p class="type">H</p>
    <h2 class="name">Name</h2>
    <p class="strain-info">No Info Available</p>
    <p class="review"><a class="button radius small black review-form-lb" href="#review-form-lightbox">Review</a></p>
</div>

For instance when I run the code below, which basically adds a class to the element clicked, it takes several seconds for the element to enlarge.

$container.on( 'click', '.element', function() {
  $( this ).toggleClass('large');
  $container.isotope('layout');
});

Another example is if I have a button group that contains several options to filter the gallery, again it takes several seconds. Filter JS:

$('#filters').on( 'click', '.button', function() {
  var $this = $(this);
  // get group key
  var $buttonGroup = $this.parents('.button-group');
  var filterGroup = $buttonGroup.attr('data-filter-group');
  // set filter for group
  filters[ filterGroup ] = $this.attr('data-filter');
  // combine filters
  var filterValue = '';
  for ( var prop in filters ) {
    filterValue += filters[ prop ];
  }
  // set filter for Isotope
  $container.isotope({ filter: filterValue });
});

Here is the variable for $container

// init Isotope
var $container = $('.isotope').isotope({
  itemSelector: '.element',
  layoutMode: 'fitRows',
  filter: function() {
    return qsRegex ? $(this).text().match( qsRegex ) : true;
  }
});

I should note that the above code works great when there is a small number of items. How could I improve the performance of the gallery?

I should note that in version 1 of Isotopes I have the same gallery and it works fine. I am upgrading because of enhanced abilities of v2 of isotopes.

Here is the live site - IMPORTANT! - This site is for a marijuana dispensary in Colorado, USA. Website may not be appropriate for work.

Update

I tried changing all divs to li elements. Didn't see much improvement.

I tried turning on hardware acceleration per mfirdaus's answer but it didn't appear to work.

Note: The above link to the live site is a stripped down version where I removed everything not required for the Isotope gallery. It is slightly faster than when I originally posted this, however, it needs to be more responsive. Try using it on a tablet and it takes several seconds for the Isotope item to respond.

Update 2

Version One of Isotopes performs much better with a large gallery and I ended up using version one because of this.

like image 857
L84 Avatar asked May 23 '14 01:05

L84


3 Answers

I have a suggestion. First of all, you can add the css property transform:translate3d(0,0,0) to your .element css selector. This turns on hardware acceleration and should speed up the page reflow. This is a tip often used to make highly animated pages smoother, especially on mobile. so something ilke:

.element {
  ...
  /* add this. prefixes for compabtibility */
  transform:translate3d(0,0,0);
  -webkit-transform:translate3d(0,0,0);
  -moz-transform:translate3d(0,0,0);
}

In my tests it seems to speed it up quite nicely.

Another suggestion would be to replace .on("click") with .on("mousedown"). click fires after the mouse is released whereas mousedown would fire immediately after the user press their mouse. there is like a ~0.2s difference but it's somewhat noticable.

Here's an example made from isotope's default example which has 400 elements.


Update

After testing out touchstart, it kinda helps but it fires even when scrolling. So this helps only if you disable scroll/have fixed viewport. Maybe you could add iScroll, but I reckon that's even more bloat.

One option you could try is to force an element to resize first, before waiting for isotope to recalculate the positions. We can do this by using setTimeout on the isotope refresh. Not sure if the behaviour is acceptable but this will allow the user to get feedback faster. So your toggling code will be something like:

$container.on( 'click', '.element',function() {
  $( this ).toggleClass('large');
  setTimeout(function(){ $container.isotope('layout'); },20); //queue this later
});

Demo. Tested this on my android device and there seemed to be an improvement.

like image 88
mfirdaus Avatar answered Oct 12 '22 23:10

mfirdaus


Well, the author of the isotope plugin recommends 50 items max if you're doing filtering or sorting. You have 400!.. So I'm afraid you'll need to decrease the num. of items for better performance.

Some side notes:

  • Try touchstart or mousedown events instead of click for a bit more quick responses on mobile.
  • Check for Object.keys(filters).forEach performance on mobile (instead of for in)..
  • jQuery's closest() method might be faster than parents() in some browsers.
like image 27
Onur Yıldırım Avatar answered Oct 13 '22 01:10

Onur Yıldırım


Instead of using toggleClass, try directly manipulating the style.

$( this ).css({ /* styles for large */});
like image 24
Michael Benin Avatar answered Oct 12 '22 23:10

Michael Benin