Created
July 18, 2010 19:10
-
-
Save mape/480627 to your computer and use it in GitHub Desktop.
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
| // Find: id-127948013911961470649950206280740122937131673124230331368744372 | |
| // Array | |
| { id: 'id-127948013911961470649950206280740122937131673124230331368744372' | |
| , name: 'mape-id-127948013911961470649950206280740122937131673124230331368744372' | |
| } | |
| // Fetch using array: 30ms | |
| // Object | |
| { name: 'mape-id-127948013911961470649950206280740122937131673124230331368744372' } | |
| // Fetch using object: 0ms | |
| // Manager | |
| id-127948013911961470649950206280740122937131673124230331368744372 | |
| // Fetch using manager: 33ms |
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
| console.timeStore = {}; | |
| console.time = function(str) | |
| { | |
| console.timeStore[str] = new Date().getTime(); | |
| }; | |
| console.timeEnd = function(str) | |
| { | |
| var diff = new Date().getTime()-console.timeStore[str]; | |
| console.log(str+': '+diff+'ms'); | |
| }; | |
| /*----------------------------------------------- | |
| Connection Manager | |
| -----------------------------------------------*/ | |
| function Manager(){ | |
| this._head = null; | |
| this._tail = null; | |
| this._length = 0; | |
| }; | |
| Object.defineProperty(Manager.prototype, "length", { | |
| get: function(){ | |
| return this._length; | |
| } | |
| }); | |
| Manager.prototype.attach = function(id, client){ | |
| var connection = { | |
| id: id, | |
| _next: null, | |
| client: client | |
| }; | |
| if(this._length == 0) { | |
| this._head = connection; | |
| this._tail = connection; | |
| } else { | |
| this._tail._next = connection; | |
| this._tail = connection; | |
| } | |
| ++this._length; | |
| }; | |
| Manager.prototype.detach = function(id, callback){ | |
| var previous = current = this._head; | |
| while(current !== null){ | |
| if(current.id === id){ | |
| previous._next = current._next; | |
| this._length--; | |
| if(current.id === this._head.id){ | |
| this._head = current._next; | |
| } | |
| if(current.id === this._tail.id){ | |
| this._tail = previous; | |
| } | |
| break; | |
| } else { | |
| previous = current; | |
| current = current._next; | |
| } | |
| } | |
| delete current, previous; | |
| callback(); | |
| }; | |
| Manager.prototype.find = function(id, callback){ | |
| var current = this._head; | |
| while(current !== null){ | |
| if(current.id === id){ | |
| callback(current.id); | |
| break; | |
| } else { | |
| current = current._next; | |
| } | |
| } | |
| }; | |
| Manager.prototype.forEach = function(callback){ | |
| var current = this._head; | |
| while(current !== null){ | |
| callback(current.client); | |
| current = current._next; | |
| } | |
| }; | |
| var man = new Manager(); | |
| var userIds = []; | |
| // Create dummy ids | |
| for (var i=0; i < 1000000; i++) { | |
| userIds.push('id-'+new Date().getTime()+Math.random()*100000000000000000+''+(Math.random()*100000000000000000)+''+(Math.random()*100000000000000000)); | |
| }; | |
| // Populate array with ids and users attached | |
| var usersArr = [] | |
| for (var i=0,l=userIds.length; i < l; i++) { | |
| usersArr.push({ | |
| 'id': userIds[i] | |
| , 'name': 'mape-'+userIds[i] | |
| }); | |
| }; | |
| // Populate object with ids and users attached | |
| var usersObj = [] | |
| for (var i=0,l=userIds.length; i < l; i++) { | |
| usersObj[userIds[i]] = { | |
| 'name': 'mape-'+userIds[i] | |
| } | |
| }; | |
| // Populate manager with ids and users attached | |
| for (var i=0,l=userIds.length; i < l; i++) { | |
| man.attach(userIds[i], { | |
| 'name': 'mape-'+userIds[i] | |
| }); | |
| }; | |
| // Generate an id to find | |
| var randomId = userIds[Math.floor(Math.random()*userIds.length)]; | |
| console.log('\033[33m\nFind: '+randomId+'\033[39m'); | |
| console.log('\033[31m\nArray\033[39m'); | |
| console.time('Fetch using array') | |
| for (var i=0,l=userIds.length; i < l; i++) { | |
| if (usersArr[i].id === randomId) | |
| { | |
| console.dir(usersArr[i]); | |
| break; | |
| } | |
| }; | |
| console.timeEnd('Fetch using array') | |
| console.time('Fetch using object') | |
| console.log('\033[31m\nObject\033[39m'); | |
| console.dir(usersObj[randomId]); | |
| console.timeEnd('Fetch using object') | |
| console.time('Fetch using manager') | |
| console.log('\033[31m\nManager\033[39m'); | |
| man.find(randomId, function(id){console.log(id);}) | |
| console.timeEnd('Fetch using manager') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment