0

Backbone provides options to select models from collections both by ID (a unique identifier attribute assigned to every model) and by index. Which of these is the fastest way to access items from a collection?

Cracking open Backbone.js, I can see that collection.get(id) (the select-by-ID function) uses a simple object-literal look-up and collection.at(index) (the select-by-index function) uses a simple array look-up.

from Backbone.js:

collection.get(id):

// Get a model from the set by id.
get: function(obj) {
    if (obj == null) return void 0;
    return this._byId[obj] || this._byId[obj.id] || this._byId[obj.cid];
}

collection.at(index):

// Get the model at the given index.
at: function(index) {
    return this.models[index];
}

Because of this, the answer to this question should be directly tied to which is faster - array access or object literal access (in this case assuming that .get is used in its first iteration, where it's sent an ID, and not a model with an ID, or CID on it).

Sandy Gifford
  • 7,219
  • 3
  • 35
  • 65
  • That seems like a sort of harsh down-vote – Sandy Gifford Apr 01 '15 at 17:25
  • This is something pretty easy to test yourself - might be the reason for the downvote. – Allan Nienhuis Apr 01 '15 at 17:27
  • @AllanNienhuis This was an "Answer your own question" question. I made the test case and figured I'd share my discovery with Stack. I assumed it would look silly to answer my own question in the question **and** the answer. – Sandy Gifford Apr 01 '15 at 17:29
  • ah, I didn't notice that. Perhaps it was a commentary about premature optimization then. (I can't remember the last time this sort of optimization mattered to me in any of my projects). Sure would be nice to for people to provide comments on their downvotes... – Allan Nienhuis Apr 01 '15 at 18:35
  • @AllanNienhuis I actually opened a Meta question about this and got my butt pretty squarely handed to me. I've refined the question a bit to try and give it better context. It's still not great (as was explained to me at length earlier) but it's better. – Sandy Gifford Apr 01 '15 at 18:38

1 Answers1

1

According to this JSPerf, select by index (using collection.at(index)) is generally faster than select by ID (using collection.get(id)) but by how much varies widely by browser. On Chrome and at least one of the versions of Firefox I tested, the difference is negligible, but still systematically in favor of select by index; in IE11, however, select by index is consistently (and almost exactly) twice as fast.

The moral of the story here is to use select by index whenever possible; hashed object retrieval is fast and convenient, but lacks the raw efficiency of indexed look-ups.

To access objects from a hash, Javascript engines must go through an additional look-up step, this in addition the overall complexity of objects make them a less-than-ideal choice for any script where performance is a consideration.

Community
  • 1
  • 1
Sandy Gifford
  • 7,219
  • 3
  • 35
  • 65
  • To me it seems more like it doesn't actually matter except when using IE11, so why bother refactoring code for just one version of just one browser? – Olfan Jun 12 '15 at 14:06