Backbone associations speed suit

JavaScript performance comparison

Revision 2 of this test case created by Jaynti Kanani

Info

Backbone-associations

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="//underscorejs.org/underscore.js">
</script>
<script src="//backbonejs.org/backbone.js">
</script>
<script src="//dhruvaray.github.com/backbone-associations/backbone-associations.js">
</script>
<script src="//raw.github.com/PaulUithol/Backbone-relational/master/backbone-relational.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var _getDepartments = function(){
        var loc1 = {
            add1 : "P.O Box 3899",
            zip: "94404",
            state : "CA"
        };
        var loc2 = {
            add1 : "P.O Box 4899",
            zip: "95502",
            state : "CA"
        };
        return [
            {
                name : 'R&D',
                number : '23',
                locations : [
                    loc1,
                    loc2
                ]
            },
            {
                name : 'Marketing',
                number : '24',
                locations : [
                    loc1,
                    loc2
                ]
            }
        ];
    };
    var getEmployee = function(count) {
        !count && (count = 10);
        var result = {
          name: 'comp' + count,
          employees: []
        };
        var departmentData = _getDepartments ();
        for (var i = 0; i < count; i++) {
          var emp = {
            fname: 'fname' + i,
            lname: 'lname' + i,
            age: (function() {
              var age = parseInt(Math.random() * 100);
              return age < 20 ? age + 20 : age > 65 ? 65 : age;
            })(),
            sex: (function() {
              return (parseInt(Math.random() * 100) % 2) ? 'M' : 'F';
            })(),
            works_for: (function() {
              return departmentData[(parseInt(Math.random() * 100) % 2)];
            })()
          };
          result.employees.push(emp);
        }
        return result;
    }  
    // Associated Model  
    var associatedModel = {};
    associatedModel.Location = Backbone.AssociatedModel.extend({
      defaults: {
        add1: "",
        add2: null,
        zip: "",
        state: ""
      }
    });
   
    associatedModel.Department = Backbone.AssociatedModel.extend({
      relations: [{
        type: Backbone.Many,
        key: 'locations',
        relatedModel: associatedModel.Location
      }],
      defaults: {
        name: '',
        locations: [],
        number: -1
      }
    });
   
    associatedModel.Employee = Backbone.AssociatedModel.extend({
      relations: [{
        type: Backbone.One,
        key: 'works_for',
        relatedModel: associatedModel.Department
      }],
      validate: function(attr) {
        return (attr.sex && attr.sex != "M" && attr.sex != "F") ? "invalid sex value" : undefined;
      },
      defaults: {
        sex: 'M',
        //{F,M}
        age: 0,
        fname: "",
        lname: "",
        works_for: {}
      }
    });
   
    associatedModel.Company = Backbone.AssociatedModel.extend({
      relations: [{
        type: Backbone.Many,
        key: 'employees',
        relatedModel: associatedModel.Employee
      }],
      defaults: {
        name: '',
        employees: []
      }
    });
   
    // Relational Model    
    var relationalModel = {};
    relationalModel.Location = Backbone.RelationalModel.extend({
      defaults: {
        add1: "",
        add2: null,
        zip: "",
        state: ""
      }
    });
   
    relationalModel.Department = Backbone.RelationalModel.extend({
      relations: [{
        type: Backbone.HasMany,
        key: 'locations',
        relatedModel: relationalModel.Location
      }],
      defaults: {
        name: '',
        locations: [],
        number: -1
      }
    });
   
    relationalModel.Employee = Backbone.RelationalModel.extend({
      relations: [{
        type: Backbone.HasOne,
        key: 'works_for',
        relatedModel: relationalModel.Department
      }],
      validate: function(attr) {
        return (attr.sex && attr.sex != "M" && attr.sex != "F") ? "invalid sex value" : undefined;
      },
      defaults: {
        sex: 'M',
        //{F,M}
        age: 0,
        fname: "",
        lname: "",
        works_for: {}
      }
    });
   
    relationalModel.Company = Backbone.RelationalModel.extend({
      relations: [{
        type: Backbone.HasMany,
        key: 'employees',
        relatedModel: relationalModel.Employee
      }],
      defaults: {
        name: '',
        employees: []
      }
    });
   
    var employeeCollection = getEmployee(100);
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
backbone-associations
var company = new associatedModel.Company();
company.set(employeeCollection);
pending…
backbone-relational
var company = new relationalModel.Company();
company.set(employeeCollection);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment