Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Simple string compare vs SHA-1 hash
(version: 0)
Worst case large string comparison.
Comparing performance of:
=== vs SHA-1
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let s10MB = "0123456789".repeat(1000*1000); var strings10MB = Array.from(Array(20)).map(o=>s10MB + String.fromCharCode(32+~~(Math.random()*96))); function hashString(input) { var encoder = new TextEncoder(); return crypto.subtle.digest('SHA-1', encoder.encode(input)).then(function (hashBuffer) { var hashArray = Array.from(new Uint8Array(hashBuffer)); return hashArray.map(byte => byte.toString(16).padStart(2, '0')).join(''); }); }
Tests:
===
const s1 = strings10MB[~~(strings10MB.length*Math.random())]; const s2 = strings10MB[~~(strings10MB.length*Math.random())]; const b = s1 === s2;
SHA-1
const s1 = strings10MB[~~(strings10MB.length*Math.random())]; const s2 = strings10MB[~~(strings10MB.length*Math.random())]; const hash1 = hashString(s1); const hash2 = hashString(s2); const b = hash1 === hash2;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
===
SHA-1
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Android 13; Mobile; rv:139.0) Gecko/139.0 Firefox/139.0
Browser/OS:
Firefox Mobile 139 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
===
1005.7 Ops/sec
SHA-1
34.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Purpose** The goal of this benchmark is to compare two approaches for checking if two strings are equal: a simple string comparison using the `===` operator, and a hash-based approach using the SHA-1 algorithm. The benchmark aims to determine which approach is faster and more efficient in terms of execution time. **Options Compared** There are two options being compared: 1. **Simple String Comparison (`===`)**: This method uses the equality operator (`===`) to compare two strings. It's a straightforward approach that works well for most cases, but may be slow or inaccurate for very large or complex strings. 2. **Hash-Based Approach (SHA-1)**: This method calculates the SHA-1 hash of each string and compares the resulting hashes using the `===` operator. Hash-based approaches are often used in situations where data integrity is critical, as small changes to a string can result in significantly different hashes. **Pros and Cons** **Simple String Comparison (`===`)** Pros: * Easy to implement and understand * Fast for short or simple strings * Accurate for most cases Cons: * Slow or inaccurate for very large or complex strings * May not work well with Unicode or non-ASCII characters **Hash-Based Approach (SHA-1)** Pros: * Can handle large or complex strings more efficiently * Can be used to detect data corruption or tampering * Works well with Unicode or non-ASCII characters Cons: * Requires calculating the SHA-1 hash, which can be computationally expensive * May not work well if the input string is very short (e.g., a single character) **Library and Purpose** The `crypto` library is used in the benchmark to implement the SHA-1 algorithm. The `crypto.subtle.digest()` function is used to calculate the hash of a given input string. **Special JS Feature/Syntax** None mentioned, but it's worth noting that this benchmark uses ES6+ syntax (e.g., arrow functions, template literals) and assumes the use of modern JavaScript features. **Other Considerations** When choosing between these two approaches, consider the following: * Use the simple string comparison (`===`) for most cases where data integrity is not critical. * Use the hash-based approach (SHA-1) when working with large or complex strings, or when data integrity is crucial. * Be aware of the potential trade-offs in terms of execution time and accuracy. **Alternatives** Other alternatives to consider: * Other hashing algorithms (e.g., MD5, SHA-256) * More advanced string comparison techniques (e.g., Levenshtein distance, Jaro-Winkler distance) * Using a third-party library or framework for string comparison and hashing.
Related benchmarks:
string-hashcode
string-hashcode2
string-hashcode3
TextEncoder vs String hash v2
Comments
Confirm delete:
Do you really want to delete benchmark?