rev-rel (v8)

Revision 8 of this benchmark created by cybermage on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="https://underscorejs.org/underscore.js">
</script>
<script src="http://backbonejs.org/backbone-min.js">
</script>
<script src="https://dhruvaray.github.io/backbone-associations/backbone-associations.edge.js">
</script>
<script src="https://pauluithol.github.io/Backbone-relational/backbone-relational.js">
</script>

Setup

Benchmark.prototype.setup = function() {

   var addAnimals = function(zoo, count, auto) {
        var result = [];
        for (var i = 0; i < count; i++) {
            result.push(auto ? {species: 'species' + i} : {species: 'species' + i, livesIn:zoo });
        }
        return result;
    };

    var _populateZoo = function(zoos){
        var result = [];
        for (var i = 0; i < zoos; i++) {
            result.push({name: i});
        }
        return result;
    };

    var setupCounties = function(countries, zoos){
        var result = [];
        for (var i = 0; i < countries; i++) {
            result.push({name: i, zoos:_populateZoo(zoos)});
        }
        return result;
    };

    // Associated Model
    var associatedModel = {};

    associatedModel.Zoo = Backbone.AssociatedModel.extend({
        relations: [{
            type: Backbone.Many,
            key: 'animals',
            relatedModel: function(){return associatedModel.Animal}
        }]
    });
    associatedModel.Animal = Backbone.AssociatedModel.extend({
        relations: [{
            type: Backbone.One,
            key: 'livesIn',
            relatedModel: function(){return associatedModel.Zoo}
        }]
    });

    associatedModel.Country = Backbone.AssociatedModel.extend({
        relations: [{
            type: Backbone.Many,
            key: 'zoos',
            relatedModel: associatedModel.Zoo
        }]
    });


    var relationalModel = {};

    relationalModel.Animal = Backbone.RelationalModel.extend({

    });

    relationalModel.Zoo = Backbone.RelationalModel.extend({
        relations: [{
            type: Backbone.HasMany,
            key: 'animals',
            relatedModel: relationalModel.Animal,
            reverseRelation: {
                key: 'livesIn',
                includeInJSON: 'id'
            }
        }]
    });

    relationalModel.Country = Backbone.RelationalModel.extend({
        relations: [{
            type: Backbone.HasMany,
            key: 'zoos',
            relatedModel: relationalModel.Zoo
        }]
    });


  }

Test runner

Ready to run.

Testing in
TestOps/sec
BB-Associations
var c1 = new Backbone.Collection(setupCounties(1,10),{model:associatedModel.Country});
        c1.each(function(country){
            country.get('zoos').each(function(zoo){
                zoo.set('animals',addAnimals(zoo,50))
            });
        })
ready
BB-Relational
var c1 = new Backbone.Collection(setupCounties(1,10),{model:relationalModel.Country});
        c1.each(function(country){
            country.get('zoos').each(function(zoo){
                zoo.set('animals',addAnimals(zoo,50,true))
            });
        })
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 4: published by Dhruva Ray on
  • Revision 5: published by Dhruva Ray on
  • Revision 7: published by cybermage on
  • Revision 8: published by cybermage on