Skip to content

Instantly share code, notes, and snippets.

@gblache
Created June 13, 2012 06:08
Show Gist options
  • Save gblache/2922169 to your computer and use it in GitHub Desktop.
Save gblache/2922169 to your computer and use it in GitHub Desktop.
Iterative vs Bulk add in Backbone
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Backbone Collection Demo</title>
</head>
<script src="http://underscorejs.org/underscore.js" type="text/javascript"></script>
<script src="http://backbonejs.org/backbone.js" type="text/javascript"></script>
<script type="text/javascript">
var collection = new Backbone.Collection();
// Iterative add.
var start = new Date().getTime();
_.times(1000, function() {
var model = new Backbone.Model({id: _.uniqueId()});
collection.add(model);
});
var finish = new Date().getTime();
console.log(finish - start);
// Bulk add.
var start = new Date().getTime();
var models = []
_.times(1000, function() {
var model = new Backbone.Model({id: _.uniqueId()});
models.push(model);
});
collection.add(models);
var finish = new Date().getTime();
console.log(finish - start);
</script>
<body></body>
</html>
@gblache
Copy link
Author

gblache commented Jun 13, 2012

Console output

  • 136
  • 17

@chchrist
Copy link

collection.add() will trigger model events. Does models.push() also trigger events?

@yard2010
Copy link

@chcrist I don't think so.. models.push is array.push which is a native JS method that doesn't support events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment