lucaong / jQCloud

jQuery plugin for drawing neat word clouds that actually look like clouds
MIT License
648 stars 293 forks source link

Best practices for performance at scale? #22

Open pleonard212 opened 12 years ago

pleonard212 commented 12 years ago

For somewhat improbable reasons, I have some pages with 100 to 200 wordclouds being invoked simultaneously. At this scale, setting delaymode to true or false doesn't make much of a difference (at around 30, it is helpful to have it to true.)

I'm wondering if there are any obvious things I can do to lessen the burden this puts on the javascript engine. I'm currently invoking all of the wordclouds with the document.ready function, and addressing the divs by id, which I understand is the fastest way for jQuery to work.

I've thought of trying to stagger the requests through some kind of timing loop, or changing the delay value in delayedMode, but thought I'd check in here first to see if anyone had any ideas. Thanks!

lucaong commented 12 years ago

If not rendering the clouds in parallel is not an issue, an idea could be to concatenate clouds using a callback function. Something like this:

<div id="cloud_0"></div>
<div id="cloud_1"></div>
<div id="cloud_2"></div>
<!-- and so on... -->
var wordArrays = [...] // Array of word arrays, one for each cloud

// Recursive function to render clouds one after another
var renderNextCloud = function(cloudCounter) {
  var container = $("#cloud_"+cloudCounter);
  if(container.length === 0) { return }
  container.jQCloud(wordArrays[cloudCounter], {
    afterCloudRender: function(){
      renderNextCloud(++cloudCounter);
    }
  });
});

// When ready, start rendering one cloud after another
$(function() {
  renderNextCloud(0);
});
pleonard212 commented 12 years ago

Thanks very much -- this seems to have improved things quite a bit. What I eventually ended up doing was using the jquery-appear plugin: http://code.google.com/p/jquery-appear/ ... which ensures that the browser only has to process those div's that are visible on the screen at one time.

The essential problem may be how FireFox (even version 12) handles math functions... they try to guess which of two JIT compilers to use, and perhaps sometimes guess wrong. Regardless, it's amazing the difference between what Chrome/Safari can handle nowadays, which routinely breaks FireFox's JS interpreter.