Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
WordsToNumbers2
(version: 1)
Comparing performance of:
wordsInNumbers1 vs wordsInNumbers2
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var wordsInNumbers1 = (str, arr) => { const uniqueSizes = [] const subStrings = new Set() return arr.reduce((acc, s) => { if (uniqueSizes.indexOf(s.length) === -1) { for (let i = 0; i < (str.length - s.length + 1); i++) { subStrings.add(str.substr(i, s.length)) } uniqueSizes.push(s.length) } if (subStrings.has( s.split('').map(char => '22233344455566677778889999' [char.charCodeAt() - 97]).join(''))) return [...acc, s] return acc }, []) } const keyMap = { a: 2, b: 2, c: 2, d: 3, e: 3, f: 3, g: 4, h: 4, i: 4, j: 5, k: 5, l: 5, m: 6, n: 6, o: 6, p: 7, q: 7, r: 7, s: 7, t: 8, u: 8, v: 8, w: 9, x: 9, y: 9, z: 9, }; const wordToNumbers = (word) => { const letters = Array.from(word); return letters.reduce((numberWord, letter) => { const numberLetter = keyMap[letter]; return `${numberWord}${numberLetter}`; }, ""); }; var wordsInNumbers2 = (str, arr) => { return arr.filter((word) => { const numberWord = wordToNumbers(word); const re = new RegExp(numberWord, "i"); return re.test(str); }); };
Tests:
wordsInNumbers1
wordsInNumbers1('223786597355927753757922849683', ['curl', 'apple', 'frog', 'cat', 'cool', 'egg', 'bat', 'den'])
wordsInNumbers2
wordsInNumbers2('223786597355927753757922849683', ['curl', 'apple', 'frog', 'cat', 'cool', 'egg', 'bat', 'den'])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
wordsInNumbers1
wordsInNumbers2
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 break down the benchmark and its options. **Benchmark Definition** The benchmark, named "WordsToNumbers2", tests two different JavaScript functions: `wordsInNumbers1` and `wordsInNumbers2`. These functions are designed to convert words into numbers based on a predefined mapping of letters to numbers. **Function 1: `wordsInNumbers1`** This function takes an input string `str` and an array `arr` as arguments. It returns the filtered array containing only the elements that match the pattern defined in `str`. The pattern is created by iterating over each character in the input string, and for each character, it checks if the corresponding letter has been seen before (i.e., its length is already in the `uniqueSizes` array). If not, it adds all substrings of the input string that start at index `i` to the `subStrings` set. It also pushes the length of the current substring to the `uniqueSizes` array. The function then iterates over each word in the input array and checks if it matches the pattern defined in the input string using a regular expression. If a match is found, the word is added to the result array. **Function 2: `wordsInNumbers2`** This function takes an input string `str` and an array `arr` as arguments. It returns the filtered array containing only the elements that match the pattern defined in the input string using a regular expression. The pattern is created by mapping each letter to its corresponding number using the `keyMap` object. **Options compared** The two functions compare different approaches to achieve the same goal: * **String manipulation vs. Regular Expressions**: `wordsInNumbers1` uses string manipulation (substr, Set, and push) to create the pattern, while `wordsInNumbers2` uses regular expressions to match the pattern. * **Linear search vs. Pattern matching**: Both functions have a linear search component, but `wordsInNumbers2` uses a regular expression to quickly match the entire word against the pattern. **Pros and Cons** Here are some pros and cons of each approach: * **`wordsInNumbers1`**: + Pros: More flexible and customizable, as the user can modify the pattern by adjusting the `uniqueSizes` array. + Cons: Slower due to string manipulation and more complex logic. * **`wordsInNumbers2`**: + Pros: Faster due to regular expression matching and simpler logic. + Cons: Less flexible and less customizable, as the user is limited to using the predefined `keyMap` object. **Library usage** The benchmark uses two libraries: * **`Set`**: Used in both functions to store unique substrings or letters. * **`RegExp`**: Used only in `wordsInNumbers2` to match the pattern against the input string. **Special JavaScript feature** There is no special JavaScript feature used in these benchmarks. However, if we were to extend this benchmark, we might consider using features like async/await, Promises, or Web Workers to improve performance and scalability. **Other alternatives** If you want to explore alternative approaches, here are some ideas: * Use a different data structure, such as an array of strings or an object with letter-number mappings. * Implement the pattern matching logic using a different algorithm, such as dynamic programming or greedy algorithms. * Use a library like `lodash` or `underscore` for string manipulation and regular expression matching. Keep in mind that each alternative approach may have its own pros and cons, and some might be more suitable depending on the specific requirements of your use case.
Related benchmarks:
codewars test alex
Word split
longestCommonPrefix
Consume Object.entries vs for..in 2
Comments
Confirm delete:
Do you really want to delete benchmark?