Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hamming distance.
(version: 0)
test of hamming distance JS implementation
Comparing performance of:
riverotter vs mcdonalds
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function hammingDistance(stringA, stringB) { if (stringA.length === stringB.length) { let count = 0 for (char in stringA){ if (stringA[char] !== stringB[char]) count++ } return count } else { return 'strings are not the same length' } } function hammingDistanceForLoop(stringA, stringB) { let result = 0 if (stringA.length == stringB.length) { for (let i = 0; i < stringA.length; i++) { if (stringA[i].toLowerCase() != stringB[i].toLowerCase()) { result++ } } return result } else { throw new Error('Strings do not have equal length') } }
Tests:
riverotter
hammingDistance('riverotter', 'roderofter')
mcdonalds
hammingDistanceForLoop('McDonalds', 'mcdonalds')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
riverotter
mcdonalds
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
riverotter
321976.1 Ops/sec
mcdonalds
8629017.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested, compared, and discussed. **Benchmark Definition** The benchmark definition provides information about the test case being run on MeasureThat.net. In this case, there are two test cases: "hamming distance" and its implementation using a for loop ("hammingDistanceForLoop"). **Options Compared** Two options are compared: 1. **Hamming Distance (String Indexing)** The first option is the Hamming distance algorithm implemented using string indexing (`stringA[char] !== stringB[char]`). This approach directly compares characters at corresponding indices in both strings, which can be efficient when dealing with strings of fixed length. Pros: - Direct and straightforward implementation. - Suitable for fixed-length strings. Cons: - May not work correctly for strings of varying lengths or non-string inputs. - Can be inefficient for very large strings due to direct indexing. 2. **Hamming Distance (For Loop)** The second option is the Hamming distance algorithm implemented using a for loop (`for (let i = 0; i < stringA.length; i++)`). This approach iterates over each character in both strings, comparing them and incrementing a counter if they are different. Pros: - Suitable for strings of varying lengths. - More robust against non-string inputs or indexing errors. Cons: - More complex implementation compared to direct string indexing. - May be less efficient than direct indexing for fixed-length strings due to the overhead of loop iteration. **Special Features and Libraries** There is no special JavaScript feature or syntax used in these benchmarks, but it's worth noting that MeasureThat.net often tests other aspects of JavaScript performance, such as garbage collection, async execution, or specific library functions. **Other Alternatives** If you were to implement Hamming distance using different approaches, some alternatives could be: - **Bitwise Operations**: For fixed-length strings, bitwise operations (e.g., XOR) can be used to compare characters and count differences. - **Regular Expressions**: Using regular expressions (`RegExp`) to match characters at corresponding indices in both strings. - **Array Comparisons**: Using `Arrays.prototype.every()` or a similar method to iterate over the characters of both strings. Each alternative has its own pros and cons, which would be worth exploring if you were looking for additional optimization techniques.
Related benchmarks:
hamming distance again
large string comparison benchmark
Simple string compare vs MurmurHash on a large string
large string size comparison
Comments
Confirm delete:
Do you really want to delete benchmark?