Skip to content

Instantly share code, notes, and snippets.

@Tom32i
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom32i/f2e834361defabda4ad9 to your computer and use it in GitHub Desktop.
Save Tom32i/f2e834361defabda4ad9 to your computer and use it in GitHub Desktop.
Collection: Array vs Object?
/**
* 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);
};
/**
* 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