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)))
function compare(a, b) {
const lenA = a.length;
const lenB = b.length;
const minLen = lenA < lenB ? lenA : lenB;
var i = 0;
for (; i < minLen; ++i) {
const ca = a.charCodeAt(i);
const cb = b.charCodeAt(i);
if (ca > cb)
return 1;
else if (ca < cb)
return -1;
}
if (lenA === lenB)
return 0;
return lenA > lenB ? 1 : -1;
};
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 = strings1MB[~~(strings1MB.length*Math.random())];
const s2 = strings1MB[~~(strings1MB.length*Math.random())];
const b = compare(s1,s2);
const s1 = strings2MB[~~(strings2MB.length*Math.random())];
const s2 = strings2MB[~~(strings2MB.length*Math.random())];
const b = compare(s1,s2);
const s1 = strings10MB[~~(strings10MB.length*Math.random())];
const s2 = strings10MB[~~(strings10MB.length*Math.random())];
const b = compare(s1,s2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1MB string comparison | |
2MB string comparison | |
10MB string comparison | |
1MB string comparison+ | |
2MB string comparison+ | |
10MB string comparison+ |
Test name | Executions per second |
---|---|
1MB string comparison | 452040.8 Ops/sec |
2MB string comparison | 52613.8 Ops/sec |
10MB string comparison | 2013.2 Ops/sec |
1MB string comparison+ | 246.2 Ops/sec |
2MB string comparison+ | 113.9 Ops/sec |
10MB string comparison+ | 22.7 Ops/sec |
I'll dive into explaining what's being tested on the provided JSON.
Benchmark Definition
The benchmark is designed to measure the performance of string comparison in JavaScript. The test creates large strings with varying lengths (1MB, 2MB, and 10MB) and compares them using two approaches:
const b = s1 === s2;
function compare(s1, s2) { ... }
Comparison Approaches
There are four test cases that compare the performance of these two approaches.
This approach uses a simple equality check (===
) to compare two strings. This method is easy to implement but can be slow due to the overhead of creating string objects and comparing their lengths.
Pros:
Cons:
This approach uses a custom comparison function (compare
) to compare two strings. The function takes into account the length of each string and compares characters from left to right.
Pros:
Cons:
Library Usage
There is no explicit library usage mentioned in the benchmark definition. However, the Array.from
method and String.fromCharCode
are used to create large strings with random characters. These methods are part of the standard JavaScript API.
Special JS Features/Syntax
None of the test cases explicitly use any special JavaScript features or syntax. The code is straightforward and easy to understand.
Device and Browser Variations
The benchmark results show variations in performance across different devices (Desktop) and browsers (Chrome 123). This suggests that there may be platform-specific optimizations or differences in browser implementation that affect string comparison performance.
In summary, the benchmark tests the performance of simple equality checks versus custom comparison functions for string comparison in JavaScript. The test highlights the importance of choosing an efficient approach to string comparison, especially when working with large strings.