jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Looking for the fastest hash function to get a unique id of a string.
function bitwise(str){
var hash = 0, len = str.length
if (len == 0) return hash;
for (var i = 0; i < len; i++) {
char = str.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
}
return hash;
}
function numbers(str) {
var hash = 0, len = str.length
if (len == 0) return hash;
for (var i = 0; i < len; i++) {
hash = hash * 31 + str.charCodeAt(i);
}
return hash;
}
function bitwiseconv(str){
str=''+str;
var hash = 0, len = str.length;
if (len == 0) return hash;
for (var i = 0; i < len; i++) {
hash = hash * 33 ^ str.charCodeAt(i);
}
return hash|0;
}
function numbersconv(str) {
var hash = 0, len = str.length
if (len == 0) return hash;
for (var i = 0; i < len; i++) {
hash = hash * 31 + str.charCodeAt(i);
hash = hash & hash;
}
return hash;
}
function bitwiseReduce(str){
return str.split("").reduce(function(a,b)
{a=((a<<5)-a)+b.charCodeAt(0);return a&a}
,0);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Bitwise based function (No integer conversion) |
| ready |
Number based function (No integer conversion) |
| ready |
Bitwise based function |
| ready |
Number based function |
| ready |
Reduce + bitwise |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.