Last active
August 29, 2015 14:04
-
-
Save joa/91a0b51d73f7c4d8c10c 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
// this will optimize-deoptimize leading to v8 giving | |
// up on the function | |
String.prototype.hashCode = function() { | |
var n = this.length; | |
var hash = 0; | |
if(n != 0) { | |
for(var i = 0; i < n; ++i) | |
hash = Math.imul(31, hash) + this.charCodeAt(i) | 0; | |
return hash; | |
} | |
return 0; | |
}; | |
// this works just fine | |
function hashCode(s) { | |
var n = s.length; | |
var hash = 0; | |
if(n != 0) { | |
for(var i = 0; i < n; ++i) | |
hash = Math.imul(31, hash) + s.charCodeAt(i) | 0; | |
return hash; | |
} | |
return 0; | |
} | |
var n = 0; | |
for(var i = 0; i < 1000; ++i) { | |
var s = "this is my string "+Math.round(Math.random()*1000.0)+" ..."; | |
n = n + s.hashCode() | 0; | |
n = Math.imul(n, s.hashCode()); | |
// | |
//n = n + hashCode(s) | 0; | |
//n = Math.imul(n, hashCode(s)); | |
} | |
print("n: "+n); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment