Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
grouping anagram algorithm
(version: 0)
Comparing performance of:
looping to compare properties vs less loop but with char code and json stringify
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['cook', 'eat', 'ate', 'cat', 'cat', 'save', 'vase', 'state', 'taste'];
Tests:
looping to compare properties
const objStr = {}; for (const str of arr) { const strCount = {}; for (const letter of str) { strCount[letter] = (strCount[letter] || 0) + 1; } objStr[str] = strCount; } const result = []; for (const str of arr) { let indexed = null; for (const grouped of result) { const [indexedStr] = grouped; if (str.length !== indexedStr.length) { continue; } const strCount = objStr[str]; const indexedCount = objStr[indexedStr]; let isAnagram = true; for (const letter in indexedCount) { if (strCount[letter] !== indexedCount[letter]) { isAnagram = false; break; } } if (isAnagram) { indexed = grouped; break; } } if (!indexed) { result.push([str]); } else { indexed.push(str); } }
less loop but with char code and json stringify
const objStr = {}; for (const str of arr) { const strCount = {}; for (const letter of str) { const charCode = letter.charCodeAt(0); strCount[charCode] = (strCount[charCode] || 0) + 1; } const strCodes = JSON.stringify(strCount); if (objStr[strCodes]) { objStr[strCodes].push(str); } else { objStr[strCodes] = [str]; } } const hasil = Object.values(objStr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
looping to compare properties
less loop but with char code and json stringify
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):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmark test case for an algorithm that groups anagrams from a given array of strings. The goal is to find all possible groupings of strings that are anagrams of each other. **Benchmark Definition:** * `Name`: The name of the benchmark, which is "grouping anagram algorithm". * `Description`: No description is provided for this benchmark. * `Script Preparation Code`: + Initializes an array `arr` with a list of strings to be processed. + Creates an empty object `objStr` that will store the frequency count of each character in each string. * `Html Preparation Code`: No HTML code is provided. **Individual Test Cases:** The benchmark consists of two test cases: 1. **"looping to compare properties"**: This test case uses a traditional loop approach to iterate through the `arr` array and find anagrams. + Uses two nested loops to iterate through each string in `arr` and compare its frequency count with that of other strings. + If a match is found, it checks if the matched string's length is equal to the current string's length. If so, it groups them together as an anagram. 2. **"less loop but with char code and json stringify"**: This test case uses a more optimized approach by leveraging character codes and JSON stringification. + Converts each string in `arr` into a JSON object that maps character codes to their frequencies. + Uses this object to quickly look up existing anagrams and add new ones to the result. **Library Usage:** * None mentioned, but it's likely that the `JSON.stringify()` function is used to convert objects to strings for comparison. **Special JS Features/Syntax:** * No special features or syntax are explicitly used in this benchmark. * However, note that using `charCodeAt(0)` might be a subtle optimization, as it allows for faster lookup of character codes in the resulting JSON object. **Other Considerations:** * Both test cases use a similar structure to find anagrams, but with different optimization techniques. * The second test case is likely faster because it uses a more optimized data structure (the JSON object) and leverages built-in string comparison methods. **Alternatives:** There are several alternatives for optimizing this algorithm: 1. **Hashing**: Instead of comparing character frequencies, one could use hashing to quickly identify anagrams. 2. **Sorted Arrays**: Sorting the input array before processing it might help in identifying anagrams more efficiently. 3. **Suffix Tree or Suffix Array**: These data structures can be used to efficiently find anagrams in a large dataset. These alternatives would require significant changes to the benchmark code and would likely have different performance implications. In conclusion, the provided JSON represents a simple yet efficient JavaScript microbenchmark for finding anagrams from a given array of strings. The two test cases demonstrate different optimization approaches, and understanding these differences can help developers choose the most suitable approach for their specific use case.
Related benchmarks:
codewars test alex
Array.from vs Array spread with mapping of values2
string array sort comparison (simple vs localecompare)
localeCompare vs function on array sort
Merge array without duplicates (Array, Set layout)
Comments
Confirm delete:
Do you really want to delete benchmark?