function createString(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
for (let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
let length = 500
var randomWord1 = createString(length)
var randomWord2 = createString(length)
var bestWord1 = 'a'
var bestWord2 = 'a'
var badWord1 = ('a'.repeat(length) + 'g')
var badWord2 = ('x'.repeat(length) + 'g')
function checkMap(string1, string2) {
let seenMap = new Map()
for(let letter of string1) {
if(!seenMap.has(letter)) {
if(string2.includes(letter)) return "YES"
seenMap.set(letter, true)
}
}
return "NO"
}
function checkSearch(string1,string2){
for(let letter of string1){
if(string2.search(string1.substring(letter))!=-1){
return "YES";
}
}
return "NO";
}
function checkIncludes(string1,string2){
for(let letter of string1) {
if(string2.includes(letter))return "YES";
}
return "NO";
}
checkMap(bestWord1, bestWord2)
checkSearch(bestWord1, bestWord2)
checkIncludes(bestWord1, bestWord2)
checkMap(randomWord1, randomWord1)
checkSearch(randomWord1, randomWord1)
checkIncludes(randomWord1, randomWord1)
checkMap(badWord1, badWord2)
checkSearch(badWord1, badWord2)
checkIncludes(badWord1, badWord2)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map- best | |
Search- best | |
Includes -best | |
Map - random | |
Search- random | |
Includes- random | |
Map -bad | |
Search-bad | |
Includes -bad |
Test name | Executions per second |
---|---|
Map- best | 1252220.2 Ops/sec |
Search- best | 1119427.1 Ops/sec |
Includes -best | 2294449.8 Ops/sec |
Map - random | 1327567.9 Ops/sec |
Search- random | 488150.6 Ops/sec |
Includes- random | 2439126.5 Ops/sec |
Map -bad | 77933.6 Ops/sec |
Search-bad | 1691.7 Ops/sec |
Includes -bad | 29978.7 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript benchmark test case created on the MeasureThat.net platform. The benchmark measures the performance of different approaches for substring-related operations in JavaScript.
Test Cases and Approaches
There are six test cases:
checkMap(bestWord1, bestWord2)
: Tests the "map" approach, which uses a Map data structure to check if two strings contain common characters.checkSearch(bestWord1, bestWord2)
: Tests the "search" approach, which uses the indexOf()
method to search for a substring in another string.checkIncludes(bestWord1, bestWord2)
: Tests the "includes" approach, which uses the includes()
method to check if one string contains another substring.checkMap(randomString1, randomString2)
: Similar to test case 1, but with random strings instead of the "bestWord" strings.checkSearch(randomString1, randomString2)
: Similar to test case 2, but with random strings instead of the "bestWord" strings.checkIncludes(bestWord1, bestWord2)
and checkIncludes(randomString1, randomString2)
: These test cases are similar to tests 3 and 4, but use a different approach (inclusion checking).Approach Performance
The benchmark results show the performance of each approach on a Windows desktop device with Chrome 86 browser. The results are measured in executions per second.
checkMap(bestWord1, bestWord2)
: Approximately 133-136 executions per secondcheckSearch(bestWord1, bestWord2)
: Approximately 125-130 executions per secondcheckIncludes(bestWord1, bestWord2)
: Approximately 131-134 executions per secondcheckMap(randomString1, randomString2)
: Approximately 50-55 executions per second (slower than the "bestWord" version)checkSearch(randomString1, randomString2)
: Approximately 60-65 executions per second (faster than the "bestWord" version)checkIncludes(bestWord1, bestWord2)
and checkIncludes(randomString1, randomString2)
: Similar performance to test 3Conclusion
The benchmark results suggest that:
Note: These results are based on a single browser and platform configuration. The actual performance may vary depending on other factors such as JavaScript engine, hardware, and software environments.