Backbone JS comparing collections  

by ne on 2021-09-29 under Javascript

Backbone JS is a fantastic library when it comes to handy utility methods. It was indeed one of the pioneer javascript frameworks which aimed towards implementing MVP design pattern.

If you are more into the imperative coding style rather than the declarative programming style (example: Angular js), then you might love backbone js.

Here's an extension method (extending the backbone API), which will compare 2 collections and will return true or false.



  Backbone.Collection.prototype.equals = function (collectionToCompare) {
        var isEqual = true, modelIndex;
        if (this.models.length !== collectionToCompare.models.length) { //If the length of models in both the collections is different
            isEqual = false;
        else {
            for (modelIndex = 0; modelIndex < collectionToCompare.models.length; modelIndex++) { //For each model inside this collection, call model's equals method
                isEqual = this.models[modelIndex].equals(collectionToCompare.models[modelIndex]);
                if (!isEqual) {

        return isEqual;



Please note that the above method will not compare the collections deeply, meaning it will compare the values and references up until one level only.