Skip to content
Browse files

added benchmarks and fixed edge case

  • Loading branch information...
1 parent 9ae86e8 commit 319173c393856ea6680931f5fcd5df4533ad87f4 @masylum committed May 13, 2012
Showing with 129 additions and 10 deletions.
  1. +5 −1 Makefile
  2. +8 −8 backbone.subset.js
  3. +116 −0 test/benchmarks.js
  4. +0 −1 test/{test.js → specs.js}
View
6 Makefile
@@ -1,6 +1,10 @@
+MOCHA = ./node_modules/.bin/mocha --reporter spec
NODE = node
test:
- ./node_modules/.bin/mocha --reporter spec
+ @${MOCHA} test/specs.js
+
+benchmark:
+ @${NODE} test/benchmarks.js
.PHONY: test
View
16 backbone.subset.js
@@ -240,18 +240,18 @@
*/
Subset._proxyReset = function (collection, options) {
options = options || {};
- var ids
- , sieved_ids
+
+ var sieved_models
, self = this;
- if (options.model_ids) {
- ids = _.intersection(this.pluck('id'), options.model_ids);
- sieved_ids = _.pluck(_.filter(ids, function (id) {
- return self.sieve(self.get(id));
- }), 'id');
+ function getSievedModels() {
+ return _.filter(options.model_ids, function (id) {
+ var model = self.parent().get(id) || self.get(id);
+ return model && self.sieve(model);
+ });
}
- if ((!options.model_ids || this === collection || sieved_ids.length) && (!options || !options.noproxy)) {
+ if ((!options || !options.noproxy) && (!options.model_ids || this === collection || getSievedModels().length)) {
this._resetSubset(_.extend(_.clone(options), {proxied: true}));
}
};
View
116 test/benchmarks.js
@@ -0,0 +1,116 @@
+/*global it, describe, before, beforeEach*/
+var _ = require('underscore')
+ , Models = {}
+ , Collections = {}
+
+ // instances
+ , tasks, archived_tasks, urgent_tasks, project, project_tasks;
+
+GLOBAL.Backbone = require('backbone');
+require('../backbone.subset');
+
+Models.Task = Backbone.Model.extend({
+ initialize: function () {
+ this.collection = tasks;
+ }
+, isArchived: function () {
+ return !!this.get('archived');
+ }
+, isUrgent: function () {
+ return !!this.get('urgent');
+ }
+});
+
+Models.Project = Backbone.Model.extend({});
+
+Collections.Tasks = Backbone.Collection.extend({
+ model: Models.Task
+, name: 'Tasks'
+});
+
+Collections.ArchivedTasks = Backbone.Subset.extend({
+ parent: function () {
+ return tasks;
+ }
+, name: 'ArchivedTasks'
+, sieve: function (task) {
+ return task.isArchived();
+ }
+});
+
+Collections.UrgentTasks = Backbone.Subset.extend({
+ parent: function () {
+ return tasks;
+ }
+, name: 'UrgentTasks'
+, sieve: function (task) {
+ return task.isUrgent();
+ }
+});
+
+Collections.ProjectTasks = Backbone.Subset.extend({
+ beforeInitialize: function (models, options) {
+ this.project = options.project;
+ }
+, name: 'ProjectTasks'
+, parent: function () {
+ return tasks;
+ }
+, sieve: function (task) {
+ if (this.project) {
+ return this.project.id === task.get('project_id');
+ } else {
+ return false;
+ }
+ }
+});
+
+tasks = new Collections.Tasks();
+archived_tasks = new Collections.ArchivedTasks();
+urgent_tasks = new Collections.UrgentTasks();
+project = new Models.Project({id: 1});
+project_tasks = new Collections.ProjectTasks([], {project: project});
+
+// Add intial state
+(function () {
+ var start = Date.now(), end, i;
+
+ for (i = 0; i < 5000; i++) {
+ tasks.add({id: i, archived: i % 2, urgent: 0, order: i});
+ }
+
+ end = Date.now();
+
+ console.log('[bench] Add intial state: ' + (end - start));
+ console.log(tasks.length, archived_tasks.length, urgent_tasks.length, project_tasks.length);
+}());
+
+// Reset urgents
+(function () {
+ var start = Date.now()
+ , urgents = []
+ , end, i;
+
+ for (i = 5000; i < 10000; i++) {
+ urgents.push({id: i, archived: i % 2, urgent: 1, order: i, project_id: i % 2});
+ }
+
+ urgent_tasks.reset(urgents);
+ end = Date.now();
+
+ console.log('[bench] Resets urgents: ' + (end - start));
+ console.log(tasks.length, archived_tasks.length, urgent_tasks.length, project_tasks.length);
+}());
+
+// Removes project_tasks
+(function () {
+ var start = Date.now()
+ , end, i;
+
+ project_tasks.reset([]);
+
+ end = Date.now();
+
+ console.log('[bench] Removes project_tasks: ' + (end - start));
+ console.log(tasks.length, archived_tasks.length, urgent_tasks.length, project_tasks.length);
+}());
View
1 test/test.js → test/specs.js
@@ -283,7 +283,6 @@ describe('Subset', function () {
archived_tasks.bind('reset', inc('archived_tasks'));
urgent_tasks.bind('reset', inc('urgent_tasks'));
- console.log('YAI');
urgent_tasks.reset([ {id: 2, archived: 0, urgent: 1, order: 2}
, {id: 3, archived: 0, urgent: 1, order: 3}]);

0 comments on commit 319173c

Please sign in to comment.
Something went wrong with that request. Please try again.