Created
May 8, 2015 22:49
-
-
Save cdaringe/be68a21b59818fb17846 to your computer and use it in GitHub Desktop.
ampersand-model-and-collection-performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Collection = require('ampersand-collection'); | |
var Model = require('ampersand-model'); | |
var MyModel = Model.extend({ | |
props: { | |
field1: 'string', | |
field2: 'string', | |
field3: 'string', | |
field4: 'string', | |
field5: 'string' | |
} | |
}); | |
var MyColModel = Model.extend({ | |
props: { | |
field1: 'string', | |
field2: 'string', | |
field3: 'string', | |
field4: 'string', | |
field5: 'string' | |
}, | |
collections: { | |
collection1: Collection, | |
collection2: Collection, | |
collection3: Collection | |
} | |
}); | |
var myCol; | |
var NUM_COLLECTIONS = 2000; | |
var _start; | |
var i; | |
// test 0 basic models | |
_start = Date.now(); | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
new Model(); | |
} | |
console.log((Date.now() - _start).toString() + | |
' ms to generate ' + NUM_COLLECTIONS + ' models'); | |
// test 1 basic collections | |
_start = Date.now(); | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
new Collection(); | |
} | |
console.log((Date.now() - _start).toString() + | |
' ms to generate ' + NUM_COLLECTIONS + ' collections'); | |
// test 2, collection of models with basic data | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
new Collection([new Model()]); | |
} | |
console.log((Date.now() - _start).toString() + | |
' ms to generate ' + NUM_COLLECTIONS + ' collections with 1x model each'); | |
// test 3, many models, whom of which as a basic collection | |
var MyCollection = Collection.extend({ | |
model: MyModel | |
}); | |
var MyModelWithCollection = Model.extend({ | |
field1: 'string', | |
collections: { | |
subCollection: MyCollection | |
} | |
}); | |
var mmwc; | |
_start = Date.now(); | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
mmwc = new MyModelWithCollection({ | |
field1: 'string' | |
}); | |
mmwc.subCollection.set([new MyModel({ | |
field1: 'string', | |
field2: 'string', | |
field3: 'string', | |
field4: 'string', | |
field5: 'string' | |
})]); | |
} | |
console.log((Date.now() - _start).toString() + | |
' ms to generate ' + NUM_COLLECTIONS + ' models with 1x subcollection (collections hash)'); | |
// test 4, many models, whom of which as a basic collection, no collections hash | |
var ModelWithCollection = Model.extend({ | |
myCol: 'collection' | |
}); | |
var mmwc; | |
_start = Date.now(); | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
mmwc = new ModelWithCollection(); | |
mmwc.myCol = new MyCollection([new MyModel({ | |
field1: 'string', | |
field2: 'string', | |
field3: 'string', | |
field4: 'string', | |
field5: 'string' | |
})]); | |
} | |
console.log((Date.now() - _start).toString() + | |
' ms to generate ' + NUM_COLLECTIONS + | |
' models with 1x subcollection (collections attr)'); | |
// test 5, one collection, many simple models | |
var t5models = []; | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
t5models.push(new MyModel({ | |
field1: 'string', | |
field2: 'string', | |
field3: 'string', | |
field4: 'string', | |
field5: 'string' | |
})); | |
} | |
_start = Date.now(); | |
myCol = new Collection(t5models); | |
console.log((Date.now() - _start).toString() + | |
' ms to generate a collection with ' + NUM_COLLECTIONS + | |
' prebuilt models'); | |
// test 6, one collection, many models, each model with subcollections | |
var t6models = []; | |
for (i=0; i < NUM_COLLECTIONS; ++i) { | |
t6models.push({ | |
field1: 'string', | |
field2: 'string', | |
field3: 'string', | |
field4: 'string', | |
field5: 'string', | |
collection1: [{id: 1}, {id: 2}, {id: 3}], | |
collection2: [{id: 1}, {id: 2}, {id: 3}], | |
collection3: [{id: 1}, {id: 2}, {id: 3}] | |
}); | |
} | |
_start = Date.now(); | |
var MyCollection = Collection.extend({ | |
model: MyColModel | |
}); | |
myCol = new MyCollection(t6models); | |
console.log((Date.now() - _start).toString() + | |
' ms to generate a collection with ' + NUM_COLLECTIONS + | |
' models where each model has 5 str fields and 3x collections'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment