Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string-hashcode3
(version: 0)
Comparing performance of:
hashCode1 vs hashCode2 vs hashCode3 vs hashCode4
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function hashCode1(str){ var hash = 0; if (str.length == 0) return hash; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; // Convert to 32bit integer } return hash; } function hashCode2(str) { var hash = 0, i, chr; if (str.length === 0) return hash; for (i = 0; i < str.length; i++) { chr = str.charCodeAt(i); hash = ((hash << 5) - hash) + chr; hash |= 0; // Convert to 32bit integer } return hash; }; function hashCode3(str) { var hash = 5381, i = str.length; while(i) { hash = (hash * 33) ^ str.charCodeAt(--i); } return hash >>> 0; } function hashCode4(str) { var hash = 0, i = 0, length = str.length; for (; i < length; i++) { hash += str.charCodeAt(i) * (31**(length - i - 1)); hash |= 0; } return hash; }
Tests:
hashCode1
hashCode1('qwerty')
hashCode2
hashCode2('qwerty')
hashCode3
hashCode3('qwerty')
hashCode4
hashCode4('qwerty')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
hashCode1
hashCode2
hashCode3
hashCode4
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its results. **Benchmark Definition** The benchmark defines four different string hashing functions: `hashCode1`, `hashCode2`, `hashCode3`, and `hashCode4`. Each function takes a string input and returns a hash code. **Functions Comparison** Here's an overview of each function: 1. **`hashCode1`**: This function uses a simple iterative approach to calculate the hash code. It shifts and adds the ASCII values of characters, with some optimizations (e.g., `hash = ((hash<<5)-hash)+char;`). The result is converted to a 32-bit integer using bitwise operations. 2. **`hashCode2`**: This function is similar to `hashCode1`, but it uses a slightly different approach for shifting the hash value (`((hash << 5) - hash) + chr;`). The conversion to a 32-bit integer is also done differently (using `hash |= 0;`). 3. **`hashCode3`**: This function uses a more complex iterative approach, involving multiplication and bitwise XOR operations. It starts with an initial hash value of 5381 and applies the transformation to each character in the string. 4. **`hashCode4`**: This function uses a combination of multiplication and bitwise OR operations to calculate the hash code. Each character's contribution is scaled by `(31**(length - i - 1))`, which helps reduce the effect of larger characters. **Pros and Cons** Here are some pros and cons for each approach: * **`hashCode1`**: Simple, easy to implement, but may not be as effective due to potential bias or collisions. * **`hashCode2`**: Similar to `hashCode1`, but with a different shifting scheme. The difference is likely negligible in practice. * **`hashCode3`**: More complex and computationally expensive, which may impact performance. However, it could provide better hash quality and collision resistance. * **`hashCode4`**: Uses a more creative approach to scaling character contributions, which might help reduce collisions. **Library Used** None of the provided functions appear to use any external libraries beyond JavaScript's built-in functionality (e.g., `charCodeAt`, `bitwise AND`). However, if you were to optimize or improve these functions, some libraries like Crypto-JS could provide additional hashing algorithms and performance enhancements. **Special JS Features/ Syntax** The benchmark doesn't appear to utilize any special JavaScript features or syntax beyond the standard ECMAScript 5+ syntax. If it did, I wouldn't be aware of them without further information. **Alternatives** Other string hashing functions you might consider exploring include: * **FNV-1a**: A widely used, non-cryptographic hash function that's suitable for many use cases. * ** murmurhash**: A popular, fast, and relatively simple non-cryptographic hash function. * **SpongeHash**: A family of cryptographic hash functions designed for various applications. Keep in mind that the choice of hashing function ultimately depends on your specific requirements, performance constraints, and security considerations.
Related benchmarks:
Hashing-2
string-hashcode
string-hashcode2
Hashes: JavaString, DJB2, Cyr53
Comments
Confirm delete:
Do you really want to delete benchmark?