function crc32(s) {
for (var t = [], i = 0; i < 256; i++) {
for (var c = i, j = 0; j < 8; j++) {
c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1;
}
t[i] = c;
}
for (i = -1, c = -1; ++i < s.length;) {
c = c >>> 8 ^ t[c & 255 ^ s.charCodeAt(i)];
}
return ((c ^ -1) >>> 0).toString(16).toUpperCase();
}
crc32(Date.now())
function adler32(string) {
const MOD_ADLER = 65521;
let a = 1, b = 0;
for (let i = 0; i < string.length; i++) {
a = (a + string.charCodeAt(i)) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
}
return (b << 16 | a).toString(16);
}
adler32(Date.now())
function fletcher32(data) {
for (let i = sum1 = sum2 = 0; i < data.length; i++) {
sum1 = (sum1 + data.charCodeAt(i)) % 65535;
sum2 = (sum2 + sum1) % 65535;
}
return (sum2 << 16 | sum1).toString(16);
}
fletcher32(Date.now())
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
crc32 | |
adler32 | |
fletcher32 |
Test name | Executions per second |
---|---|
crc32 | 570605.5 Ops/sec |
adler32 | 30583428.0 Ops/sec |
fletcher32 | 6812790.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark measures the performance of three different hash function implementations in JavaScript: crc32, adler32, and fletcher32. These hash functions are used to generate a digital fingerprint or checksum for data.
Test Cases
Each test case represents one of the three hash function implementations:
Options Compared
The benchmark compares the performance of these three implementations under different conditions:
Pros and Cons
Here's a brief summary of the pros and cons of each implementation:
Libraries and Special JS Features
There are no specific libraries mentioned in the benchmark, as all three implementations are native JavaScript functions. However, it's worth noting that these hash functions can be used with various libraries or frameworks to provide additional functionality or features.
Special JS Feature:
None of the implementations rely on any special JavaScript features like async/await, Promises, or Web Workers, which is a good thing as it makes the benchmark more portable and run in most environments without issues.
Alternatives
Other alternatives for implementing hash functions include:
These alternatives may offer better performance or security characteristics depending on the specific use case, but they also require more computational resources.