Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance - quick test #194

Closed
pgherveou opened this issue Sep 16, 2012 · 2 comments
Closed

Performance - quick test #194

pgherveou opened this issue Sep 16, 2012 · 2 comments

Comments

@pgherveou
Copy link
Contributor

Hi Paul
I did a quick test today using the following code
to check how long it would take to load a backbone collection with 300 element each child element having a 1..n children relation loaded with 3 elements

here are the models and bootstrap data

var Child = Backbone.RelationalModel.extend({});

var Parent = Backbone.RelationalModel.extend({
  relations: [{
    type: Backbone.HasMany,
    key: 'children',
    relatedModel: Child
  }]
});

var Parents = Backbone.Collection.extend({
  model: Parent
});

// bootstrap data 
var data = [];
for (var i = 1; i <= 300; i++) {
  data.push({
    name: 'parent-' + i,
    children: [
      {id: 'p-' + i + '-c1', name: 'child-1'},
      {id: 'p-' + i + '-c2', name: 'child-2'},
      {id: 'p-' + i + '-c3', name: 'child-3'}
    ]
  });
}

The first test run in 1.3 sec on my machine

// test 1
console.log('loading...');
var start = new Date;

var parents = new Parents();
parents.on('reset', function () {
  var end = new Date;
  var secs = (end - start) / 1000;
  console.log('data loaded in ' + secs);
});
parents.reset(data);

The second test run in 0.3sec

// test 2 (run separetly)
console.log('loading...');
var start = new Date;

data.forEach(function (parent) {
  parent.children = parent.children.map(function (child) {
    return new Child(child);
  });
});

var parents = new Parents();
parents.on('reset', function () {
  var end = new Date;
  var secs = (end - start) / 1000;
  console.log('data loaded in ' + secs);
});
parents.reset(data);

I havent dugged into the code yet but can you let me know why such a difference is there something wrong in this code ?

@DouweM
Copy link
Collaborator

DouweM commented Dec 20, 2012

@PaulUithol Could you check this out?

@PaulUithol
Copy link
Owner

Well, looks like _relatedModelAdded gets called a LOT of times in cases like these; about 1 + 1 + 1 + 2 + 2 + 2 ... 300 + 300 + 300 times. I've added this test (and some variation on it) to tests.js; taking this to #231.

Not sure how we can avoid some of these calls yet, but let's try.

PaulUithol added a commit that referenced this issue Feb 28, 2013
Ref #231, #194.

- Relations bind events using `listenTo` (allowing a catch-all `stopListening`)
- removed `_relatedModelAdded` and `_relatedModelRemoved`
- simplified `tryAddRelated`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants