diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-07-29 14:20:40 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-07-29 14:20:40 -0400 |
commit | 8be85b3e48b8b8073a11e726b37429c49f45fa85 (patch) | |
tree | b9942260b1862ea7b59e42e00d9abbf57f638e0b /shell/utils.js | |
parent | 0fe89a134e1f37803c84090a1d76049042ba890c (diff) | |
download | mongo-8be85b3e48b8b8073a11e726b37429c49f45fa85.tar.gz |
fix gratuitous slowness in Map (for group) SERVER-189
Diffstat (limited to 'shell/utils.js')
-rw-r--r-- | shell/utils.js | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/shell/utils.js b/shell/utils.js index ef2889fc867..94e375cd419 100644 --- a/shell/utils.js +++ b/shell/utils.js @@ -397,10 +397,31 @@ shellHelper.show = function( what ){ if ( typeof( Map ) == "undefined" ){ Map = function(){ - this._data = []; + this._data = {}; } } +Map.hash = function( val ){ + if ( ! val ) + return val; + + switch ( typeof( val ) ){ + case 'string': + case 'number': + case 'date': + return val.toString(); + case 'object': + case 'array': + var s = ""; + for ( var k in val ){ + s += k + val[k]; + } + return s; + } + + throw "can't hash : " + typeof( val ); +} + Map.prototype.put = function( key , value ){ var o = this._get( key ); var old = o.value; @@ -413,18 +434,29 @@ Map.prototype.get = function( key ){ } Map.prototype._get = function( key ){ - for ( var i=0; i<this._data.length; i++ ){ - if ( friendlyEqual( key , this._data[i].key ) ){ - return this._data[i]; + var h = Map.hash( key ); + var a = this._data[h]; + if ( ! a ){ + a = []; + this._data[h] = a; + } + + for ( var i=0; i<a.length; i++ ){ + if ( friendlyEqual( key , a[i].key ) ){ + return a[i]; } } var o = { key : key , value : null }; - this._data.push( o ); + a.push( o ); return o; } Map.prototype.values = function(){ - return this._data.map( function(z){ return z.value } ); + var all = []; + for ( var k in this._data ){ + this._data[k].forEach( function(z){ all.push( z.value ); } ); + } + return all; } Math.sigFig = function( x , N ){ |