Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Hashes: JavaString, DJB2, Cyr53
(version: 1)
Compares different hash functions in javascript Cyrb53 - see https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
Comparing performance of:
Java String Hash vs DJB2 vs cyrb53 vs DJB2 - reduce
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var input = "Hello World!"; function javaStringHash(str) { var hash = 0; for (var i = 0; i < str.length; i++) { hash = ((hash << 5) - hash) + str.charCodeAt(i); hash |= 0; // Convert to 32bit integer } return hash; } function djb2(str) { var hash = 5381; for (var i = 0; i < str.length; i++) { hash = ((hash << 5) + hash) + str.charCodeAt(i); hash |= 0; // Convert to 32bit integer } return hash; } function djb2Reduce(str) { return str.split("").reduce(function(a, b) { a = ((a << 5) + a) + b.charCodeAt(0); return a & a; }, 0); } function cyrb53(str, seed) { var h1 = 0xdeadbeef ^ seed; var h2 = 0x41c6ce57 ^ seed; for (var i = 0; i < str.length; i++) { var ch = str.charCodeAt(i); h1 = Math.imul(h1 ^ ch, 2654435761); h2 = Math.imul(h2 ^ ch, 1597334677); } h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909); h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909); return 4294967296 * (2097151 & h2) + (h1 >>> 0); };
Tests:
Java String Hash
javaStringHash(input)
DJB2
djb2(input)
cyrb53
cyrb53(input, 0)
DJB2 - reduce
djb2Reduce(input)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Java String Hash
DJB2
cyrb53
DJB2 - reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Java String Hash
35904088.0 Ops/sec
DJB2
33919568.0 Ops/sec
cyrb53
19748430.0 Ops/sec
DJB2 - reduce
5899893.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to break down the benchmark and its results. **Benchmark Overview** The benchmark compares four hash functions: Java String Hash, DJB2 (Daniel J. Bernstein's hash function), Cyr53 (a custom hash function developed by Bryce Cherry), and a variant of DJB2 called "DJB2 - reduce". The test inputs are fixed strings ("Hello World!") and the results are measured in terms of executions per second. **Hash Functions** 1. **Java String Hash**: This is a simple hash function that uses a combination of bitwise operations to generate a 32-bit integer hash value from the input string. It works by iterating over each character in the input string, shifting and adding it to a running hash value. 2. **DJB2**: DJB2 is another simple hash function that uses a similar approach to Java String Hash but with some differences in the calculation steps. It's designed to be fast and predictable. 3. **Cyr53**: Cyr53 is a more complex hash function that uses bitwise operations, multiplication, and modular arithmetic to generate a 32-bit integer hash value from the input string. It appears to be a variant of DJB2 with some additional complexity. **DJB2 - Reduce** The "DJB2 - reduce" variant is similar to DJB2 but uses the `reduce()` method of arrays to combine the hash values of individual characters in the input string. This approach may provide better performance or more consistent results, but its effectiveness depends on the specific use case. **Library Usage** In this benchmark, none of the test cases explicitly require any external libraries beyond the built-in JavaScript functions and operators. **Special JS Features** There are no special JS features or syntaxes used in this benchmark. The hash functions all rely on basic arithmetic operations and bitwise manipulations, which are part of the standard JavaScript language. **Pros and Cons of Different Approaches** 1. **Java String Hash**: Simple and fast, but may not be as predictable as other approaches. 2. **DJB2**: Fast and reliable, but some might find it less secure than other options due to its simplicity. 3. **Cyr53**: More complex and potentially more secure, but also slower than the first two options. The "DJB2 - reduce" variant may provide better performance or more consistent results, depending on the specific use case. **Other Considerations** * Performance: The benchmark measures executions per second, which is a common metric for evaluating hash function performance. However, other factors like memory usage, cache efficiency, and parallelization may also be important in certain scenarios. * Security: While security is not explicitly evaluated in this benchmark, the choice of hash function can impact the overall security of a system or application. **Alternatives** Some alternative hash functions that might be considered for evaluation in this benchmark include: 1. **FNV-1a**: A widely used and well-regarded hash function designed to be fast and efficient. 2. ** murmurhash**: Another popular and fast hash function with good distribution properties. 3. **CityHash**: A family of hash functions optimized for memory usage and cache efficiency. These alternatives might provide different trade-offs in terms of performance, security, or other factors, depending on the specific use case.
Related benchmarks:
Hash Test
string-hashcode
string-hashcode2
string-hashcode3
Comments
Confirm delete:
Do you really want to delete benchmark?