Skip to content

Instantly share code, notes, and snippets.

@joa
Last active August 29, 2015 14:04
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 joa/91a0b51d73f7c4d8c10c to your computer and use it in GitHub Desktop.
Save joa/91a0b51d73f7c4d8c10c to your computer and use it in GitHub Desktop.
// 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