let s10MB = "0123456789".repeat(1000*1000);
var strings10MB = Array.from(Array(20)).map(o=>s10MB + String.fromCharCode(32+~~(Math.random()*96)))
const s1 = strings10MB[~~(strings10MB.length*Math.random())];
const s2 = strings10MB[~~(strings10MB.length*Math.random())];
const b = s1 === s2;
function murmurhash3_32_gc(str) {
var h1 = 0xdeadbeef;
var k1 = 0;
for (var i = 0; i < str.length; ++i) {
k1 = str.charCodeAt(i);
k1 = (k1 & 0x0000ffff) | ((k1 & 0xffff0000) >>> 16);
k1 *= 0xcc9e2d51;
k1 = (k1 << 15) | (k1 >>> 17);
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1 = h1 * 5 + 0xe6546b64;
}
h1 ^= str.length;
h1 ^= h1 >>> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >>> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;
return h1 >>> 0; // Convert to unsigned 32-bit integer
}
const s1 = strings10MB[~~(strings10MB.length*Math.random())];
const s2 = strings10MB[~~(strings10MB.length*Math.random())];
const hash1 = murmurhash3_32_gc(s1);
const hash2 = murmurhash3_32_gc(s2);
const b = hash1 === hash2;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
=== | |
murmurhash3_32_gc |
Test name | Executions per second |
---|---|
=== | 817.3 Ops/sec |
murmurhash3_32_gc | 10.3 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark consists of two test cases:
==
(Simple string compare)murmurhash3_32_gc
(MurmurHash algorithm)Test Case 1: Simple String Compare (==
)
In this test case, a large string (s10MB
) is generated and split into two random strings (s1
and s2
). The comparison of these two strings using the ===
operator is measured.
The pros of this approach are:
However, there are some cons:
Test Case 2: MurmurHash Algorithm (murmurhash3_32_gc
)
This test case uses the MurmurHash algorithm to generate a 32-bit hash value from a given string. The same large string (s10MB
) is used for both input strings, and the resulting hash values are compared.
The pros of this approach are:
However, there are some cons:
Library Used:
In both test cases, the strings10MB
array is generated using a repeating string ("0123456789"
) concatenated with random whitespace characters. This library is used to generate the large input string.
Other Considerations:
===
) in Test Case 1 may not accurately represent real-world string comparison scenarios, such as case-insensitive comparison or ignoring whitespace.Alternatives:
==
, ===
, >=
) could be used in Test Case 1 to evaluate their performance.Overall, this benchmark provides a good starting point for evaluating the performance of simple string comparison and hashing algorithms in JavaScript.