Last active
August 29, 2015 14:24
-
-
Save Tom32i/f2e834361defabda4ad9 to your computer and use it in GitHub Desktop.
Collection: Array vs Object?
This file contains 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
/** | |
* ArrayCollection | |
*/ | |
function ArrayCollection() | |
{ | |
this.ids = []; | |
this.items = []; | |
} | |
/** | |
* Clear | |
*/ | |
ArrayCollection.prototype.clear = function() | |
{ | |
this.ids.length = 0; | |
this.items.length = 0; | |
}; | |
/** | |
* Count the size of the collection | |
* | |
* @return {Number} | |
*/ | |
ArrayCollection.prototype.count = function() | |
{ | |
return this.ids.length; | |
}; | |
/** | |
* Add an element | |
* | |
* @param {Object} element | |
* | |
* @return {Boolean} | |
*/ | |
ArrayCollection.prototype.add = function(element) | |
{ | |
if (this.ids.indexOf(element.id) < 0) { | |
this.ids.push(element.id); | |
this.items[this.ids.indexOf(element.id)] = element; | |
return true; | |
} | |
return false; | |
}; | |
/** | |
* Remove an element | |
* | |
* @param {Object} element | |
* | |
* @return {Boolean} | |
*/ | |
ArrayCollection.prototype.remove = function(element) | |
{ | |
var index = this.ids.indexOf(element.id); | |
if (index >= 0) { | |
this.items.splice(index, 1); | |
this.ids.splice(index, 1); | |
return true; | |
} | |
return false; | |
}; | |
/** | |
* Does element exists in collection? | |
* | |
* @return {Boolean} | |
*/ | |
ArrayCollection.prototype.exists = function(element) | |
{ | |
return this.ids.indexOf(element.id) >= 0; | |
} | |
/** | |
* Is the collection empty? | |
* | |
* @return {Boolean} | |
*/ | |
ArrayCollection.prototype.isEmpty = function() | |
{ | |
return this.ids.length === 0; | |
}; | |
/** | |
* Get the index for the given element | |
* | |
* @param {mixed} element | |
* | |
* @return {Number} | |
*/ | |
ArrayCollection.prototype.getIndex = function(element) | |
{ | |
return this.ids.indexOf(element.id); | |
}; |
This file contains 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
/** | |
* ObjectCollection | |
*/ | |
function ObjectCollection() | |
{ | |
this.items = {}; | |
} | |
/** | |
* Clear | |
*/ | |
ObjectCollection.prototype.clear = function() | |
{ | |
for (var id in this.items) { | |
if (this.items.hasOwnProperty(id)) { | |
delete this.items[id]; | |
} | |
} | |
}; | |
/** | |
* Count the size of the collection | |
* | |
* @return {Number} | |
*/ | |
ObjectCollection.prototype.count = function() | |
{ | |
return Object.keys(this.items).length; | |
}; | |
/** | |
* Add an element | |
* | |
* @param {mixed} element | |
* | |
* @return {Boolean} | |
*/ | |
ObjectCollection.prototype.add = function(element) | |
{ | |
if (!this.items.hasOwnProperty(element.id)) { | |
this.items[element.id] = element; | |
return true; | |
} | |
return false; | |
}; | |
/** | |
* Remove an element | |
* | |
* @param {mixed} element | |
* | |
* @return {Boolean} | |
*/ | |
ObjectCollection.prototype.remove = function(element) | |
{ | |
if (this.items.hasOwnProperty(element.id)) { | |
delete this.items[element.id]; | |
return true; | |
} | |
return false; | |
}; | |
/** | |
* Does element exists in collection? | |
* | |
* @return {Boolean} | |
*/ | |
ObjectCollection.prototype.exists = function(element) | |
{ | |
return this.items.hasOwnProperty(element.id); | |
} | |
/** | |
* Is the collection empty? | |
* | |
* @return {Boolean} | |
*/ | |
ObjectCollection.prototype.isEmpty = function() | |
{ | |
return Object.keys(this.items).length === 0; | |
}; | |
/** | |
* Get the index for the given element | |
* | |
* @param {mixed} element | |
* | |
* @return {Number} | |
*/ | |
ObjectCollection.prototype.getIndex = function(element) | |
{ | |
return element.id; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment