Best practice for multiple observers

86 views
Skip to first unread message

Walery Strauch

unread,
May 29, 2013, 9:11:14 AM5/29/13
to mutation-sum...@googlegroups.com
Hello,

I have best practice question. 

I have 5 Elements that I would like to observe. There are two ways to do this:

1) One MutationSummary with 5 elements in queries
2) Five MutationSummary with 1 element in queries


The 1) looks like this:

var wasAdded = function(summary) {
  return summary.added.length;
};

new MutationSummary({
  queries: [
      { element: '.some-element' },
      { element: '.other-one' },
      { element: '#foo' },
      { element: '#bar' },
      { element: '.foobar' }
  ],
  callback: function(summaries) {
    "use strict";

  if(wasAdded(summaries[0]))
    someCallForOne();
  if(wasAdded(summaries[1]))
    
someCallForTwo();
  if(wasAdded(summaries[2]))
    
someCallForFoo();
  if(wasAdded(summaries[3]))
    
someCallForBar();
  if(wasAdded(summaries[4]))
    
someCallForFoobar();
  }
});

The 2) would be like this:

var wasAdded = function(summary) {
  return summary.added.length;
};

new MutationSummary({
  queries: [
      { element: '.some-element' }
  ],
  callback: function(summaries) {
    "use strict";

  if(!wasAdded(summaries[0]))
    return;

    // some stuff for one
  }
});

new MutationSummary({
  queries: [
      { element: '.other-one' }
  ],
  callback: function(summaries) {
    "use strict";

  if(!wasAdded(summaries[0]))
    return;

    // some stuff for other-one
  }
});

new MutationSummary({
  queries: [
      { element: '#foo' }
  ],
  callback: function(summaries) {
    "use strict";

  if(!wasAdded(summaries[0]))
    return;

    // some stuff for foo
  }
});

// ... and so on


So yeah... My question is: are there some advantages or disadvantages when I use 1) vs. 2).

I'm talking particularly about performance and memory.

Thanks :)

Best regards
Walery

Rafael Weinstein

unread,
May 29, 2013, 9:29:18 AM5/29/13
to mutation-sum...@googlegroups.com
Hi Walery,

It's an excellent question. You'll nearly always be better off using
fewer summaries with more queries (using only one is ideal). Each
MutationSummary creates a MutationObserver, so the more
MutationSummaries you create, the more work the browser is doing for
you (which means more compute and memory expense)

Also, the computation required for multiple different queries overlaps
quite a bit, so you'll save compute expense by allowing it to be
shared by a single MutationSummary.

If you absolutely need more than one, then do it, but try if you can
to minimize the number that you are using.

Hope this helps
Rafael
> --
> You received this message because you are subscribed to the Google Groups
> "mutation-summary-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mutation-summary-d...@googlegroups.com.
> To post to this group, send email to
> mutation-sum...@googlegroups.com.
> Visit this group at
> http://groups.google.com/group/mutation-summary-discuss?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages