Shopify/batman — pull request #737 by pushrax (2013-05-29 15:39:44+00:00)
Title: Don't fire so many events when adding many items to a SetIndex

#### Issue

When you load a model that `hasMany` of something, SetIndex inserts each element one by one into its `_storage` and fires a ton of events.

#### Solution

The first thing I came up with was to use another hash keyed on the same keys as the index, to kind of buffer the inserts. This works well, but creates another hash which ends up containing the exact same information, which is pretty suboptimal.

What I ended up doing (the second commit here) was making the assumption that most of the time when there are many elements with the same key, they're consecutive in the list to insert (since I think in a bunch of cases, there's actually only one key). Then, when inserting, batch consecutive elements with the same key.

Another option is to prevent events from being fired on the underlying sets with an `isolate`/`expose`-like system. This wouldn't be great either though, since then it needs to keep track of which sets have been isolated so that it can expose them later.

@kristianpd @nickjs @hornairs for review, and I have some other ideas about the event system I'd like to run by you later.