JS HashCode Custom (v2)

Revision 2 of this benchmark created by mgibsonbr on


Setup

function censor(censor) {
      var i = 0;
    
      return function(key, value) {
        if(i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value) 
          return '[Circular]'; 
    
        if(i >= 29) // seems to be a harded maximum of 30 serialized objects?
          return '[Unknown]';
    
        ++i; // so we know we aren't using the original object anymore
    
        return value;  
      }
    }
    
    Object.prototype.GetHashCode = function () {
        var s = this instanceof Object ? JSON.stringify(this, censor(this)) : this.toString();
    
        var hash = 0;
        if (s.length === 0) return hash;
        for (var i = 0; i < s.length; ++i) {
            hash = ((hash << 5) - hash) + s.charCodeAt(i);
        }
        return hash;
    };
    
    function hashString(s)
    {
        var hash = 0;
        if (s.length === 0) return hash;
        for (var i = 0; i < s.length; ++i) {
            hash = ((hash << 5) - hash) + s.charCodeAt(i);
        }
        return hash;
    }
    function hashCode4(obj, profundidade) {
        if ( !profundidade) profundidade= 0;
        var hash = 0;
        for ( var p in obj )
            hash += hashString(p) + 
                    (profundidade> 0 ? hashCode4(obj[p], profundidade-1) : 0);
        return hash;
    }
    
    var example = { ID: 1, Items: [ { Name: "X" }, { Name: "Y" } ] };
    
    var exemploEnorme1 = { valor:1, texto:"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." }
    
    var exemploEnorme2 = { valor:2, texto:"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." }
    
    var exemploEnorme3 = { valor:1, texto:"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit este cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." }
    
    var exemploSemTexto1 = { 1:2, 3:4, 5:6, 7:8, 9:10, 
                             11:12, 13:14, 15:16, 17:18, 19:20,
                             21:22, 23:24, 25:26, 27:28, 29:30,
                             31:32, 33:34, 35:36, 37:38, 39:40,
                             41:42, 43:44, 45:46, 47:48, 49:50 };
    
    var exemploSemTexto2 = { 1:987654321, 2:9.87654321 };

Test runner

Ready to run.

Testing in
TestOps/sec
GetHashCode
exemploEnorme1.GetHashCode();
exemploEnorme2.GetHashCode();
exemploEnorme3.GetHashCode();
 
ready
hashCode4 prof 0
hashCode4(exemploEnorme1, 0);
hashCode4(exemploEnorme2, 0);
hashCode4(exemploEnorme3, 0);
// Rápido, mas falha em dar hashes diferentes pra 1 e 3
ready
hashCode4 prof 1
hashCode4(exemploEnorme1, 1);
hashCode4(exemploEnorme2, 1);
hashCode4(exemploEnorme3, 1);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 2: published by mgibsonbr on