let s1MB = "0123456789".repeat(1000*100);
var strings1MB = Array.from(Array(20)).map(o=>s1MB + String.fromCharCode(32+~~(Math.random()*96)))
let s2MB = "0123456789".repeat(1000*200);
var strings2MB = Array.from(Array(20)).map(o=>s2MB + String.fromCharCode(32+~~(Math.random()*96)))
let s10MB = "0123456789".repeat(1000*1000);
var strings10MB = Array.from(Array(20)).map(o=>s10MB + String.fromCharCode(32+~~(Math.random()*96)))
let s10MB2 = "0123456789".repeat(1000*1000);
var strings10MB2 = Array.from(Array(20)).map(o=>s10MB + String.fromCharCode(32+~~(Math.random()*96)))
const s1 = strings1MB[~~(strings1MB.length*Math.random())];
const s2 = strings1MB[~~(strings1MB.length*Math.random())];
const b = s1 === s2;
const s1 = strings2MB[~~(strings2MB.length*Math.random())];
const s2 = strings2MB[~~(strings2MB.length*Math.random())];
const b = s1 === s2;
const s1 = strings10MB[~~(strings10MB.length*Math.random())];
const s2 = strings10MB[~~(strings10MB.length*Math.random())];
const b = s1 === s2;
const s1 = strings10MB2[~~(strings10MB2.length*Math.random())];
const s2 = s1 + "";
const b = s1 === s2;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1MB string comparison | |
2MB string comparison | |
10MB string comparison | |
10MB string comparison (equal) |
Test name | Executions per second |
---|---|
1MB string comparison | 21673.3 Ops/sec |
2MB string comparison | 5327.3 Ops/sec |
10MB string comparison | 461.6 Ops/sec |
10MB string comparison (equal) | 3435385.2 Ops/sec |
Measuring the performance of JavaScript strings comparisons is crucial for optimizing and improving web applications.
Benchmark Overview
The benchmark tests the execution time of three different approaches for comparing large strings in JavaScript:
===
operator (const b = s1 === s2;
).const b = s1 === s2;
).Direct Comparison
This approach is simple and straightforward but may be slow due to the following reasons:
Pros: Easy to understand and implement, no additional dependencies required.
Cons: May be slower than other approaches due to the full string comparison.
Hash-based Comparison
This approach uses a hash function to reduce the complexity of the string comparison. However, in this benchmark, it's not explicitly stated what kind of hash function is used.
Pros:
Cons: May not provide accurate results if the hash function is poorly implemented or doesn't cover all possible string cases. Also, some modern JavaScript engines might use optimized string comparisons that negate the benefits of this approach.
Concatenation with Equality Check
This approach concatenates a string with an empty string and then compares it for equality.
Pros: Can be faster than direct comparison since it involves fewer operations.
Cons:
Library Usage
In this benchmark, no explicit libraries are used. However, some JavaScript engines like V8 (used by Chrome) have optimized string comparison algorithms that can affect the performance of these approaches.
Special JS Feature or Syntax
There is no special JS feature or syntax being tested in this benchmark.
Other Alternatives
Some alternative approaches for comparing strings could include:
String.prototype.localeCompare()
method, which provides a locale-dependent string comparison.Keep in mind that these alternatives may not be relevant or applicable depending on your specific use case.