Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
duplicates in string
(version: 1)
Comparing performance of:
map: indexOf==lastIndexOf vs charMap
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
map: indexOf==lastIndexOf
function duplicateEncode(word) { const chars = word.toLowerCase(); return chars .split("") .map((char) => { if (chars.indexOf(char) === chars.lastIndexOf(char)) { return "("; } else return ")"; }) .join(""); } duplicateEncode("Lorem ipsum dolor, sit amet consectetur adipisicing elit. Consectetur iste corrupti aperiam omnis perferendis eum quidem quo necessitatibus natus! Eveniet reiciendis est reprehenderit quo ullam praesentium amet aperiam veniam illo!")
charMap
function duplicateEncode(word) { const chars = word.toLowerCase().split(""); const charMap = {}; chars.forEach((char) => { char in charMap ? charMap[char]++ : (charMap[char] = 1); }); let result = ""; chars.forEach((char) => { charMap[char] > 1 ? (result += ")") : (result += "("); }); return result; } duplicateEncode("Lorem ipsum dolor, sit amet consectetur adipisicing elit. Consectetur iste corrupti aperiam omnis perferendis eum quidem quo necessitatibus natus! Eveniet reiciendis est reprehenderit quo ullam praesentium amet aperiam veniam illo!")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map: indexOf==lastIndexOf
charMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents two benchmark test cases on MeasureThat.net, which measures the performance of JavaScript code. The tests are designed to compare the performance of different approaches for finding duplicates in a string. **Test Case 1: "map: indexOf==lastIndexOf"** This test case compares the performance of using `indexOf()` and `lastIndexOf()` methods together in an array map function. Here's what's being tested: * The `duplicateEncode` function takes a word as input, converts it to lowercase, splits it into individual characters, and maps over each character. * For each character, it checks if the character is present at the same index where it was last found using `indexOf()` and `lastIndexOf()`. If true, it returns `(`; otherwise, it returns `)`. The test case measures the performance of this approach by executing the function multiple times and recording the number of executions per second. **Test Case 2: "charMap"** This test case compares the performance of using a character map object to count occurrences of each character in the string. Here's what's being tested: * The `duplicateEncode` function takes a word as input, splits it into individual characters, and creates an empty character map object. * It then iterates over each character, incrementing its corresponding value in the map if it already exists or creating a new entry with a value of 1. * After counting occurrences, it iterates over the original array again, appending `)` to the result for each character that appears more than once and `(` for each character that appears only once. The test case measures the performance of this approach by executing the function multiple times and recording the number of executions per second. **Comparison of Approaches** Both approaches have their pros and cons: * **map: indexOf==lastIndexOf** + Pros: - Simple and easy to understand. - Uses built-in methods, which may be faster due to native implementation. + Cons: - May require more iterations over the array due to the `indexOf()` and `lastIndexOf()` calls. * **charMap** + Pros: - More efficient for large strings since it avoids repeated lookups in the array. + Cons: - Requires an extra data structure (the character map object) and iteration. **Special JS Features/ Syntax** Neither test case explicitly uses any special JavaScript features or syntax. However, if we were to interpret this code as a learning exercise, we might note that it: * Uses the `map()` method, which is a modern JavaScript feature. * Uses template literals (`\r\n`) for string interpolation, which is a recent addition to the language. **Other Alternatives** There are other approaches to finding duplicates in a string, such as: * Using regular expressions with groups and backreferences. * Using a hash table or object to store character counts. * Using `filter()` and `some()` methods to find duplicates. However, these alternatives may not be as straightforward to implement as the two tested approaches.
Related benchmarks:
string array sort comparison
string array sort comparison 2
string array sort comparison 3
endOf vs cascading equality
WordsToNumbers2
Comments
Confirm delete:
Do you really want to delete benchmark?