Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findMatchGroups old vs new
(version: 0)
Comparing performance of:
findMatchGroupsOld vs findMatchGroupsNew
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = 'abcdefghijklmnopqrstuvwxyz'; var b = 'NOP'; var locale = 'en-US' ///////////////////////////////////////////////////////////////////////////////////// var BACKSLASH = '\\'; var escapeMap = new Map( [ '\\', '^', '$', '*', '+', '?', '(', ')', '[', ']', '{', '}', '=', ':', '!', '|', ].map(char => [char, `${BACKSLASH}${char}`]) ); var escapeRegExp = (searchPhrase) => [...searchPhrase] .map(character => escapeMap.get(character) || character) .join(''); var findMatchGroupsOld = ( searchPhrase, valueToSearchOn ) => { const safeSearchPhrase = escapeRegExp(searchPhrase); const searchPhraseRegExp = new RegExp(`(.+)?(${safeSearchPhrase})(.+)?`, 'i'); const match = valueToSearchOn.match(searchPhraseRegExp); if (match) { const [, preFind = '', find = '', postFind = ''] = match; return [preFind, find, postFind]; } return [valueToSearchOn, '', '']; }; ///////////////////////////////////////////////////////////////////////////////////// var localeIndexOfLowercase = (value, searchPhrase, locale) => { const searchPhraseLowercase = searchPhrase.toLocaleLowerCase(locale); return value.toLocaleLowerCase(locale).indexOf(searchPhraseLowercase); }; var findMatchGroupsNew = (value, searchPhrase, locale) => { const index = localeIndexOfLowercase(value, searchPhrase, locale); const lastIndex = index + searchPhrase.length; if (index === -1) return { preMatch: value, match: '', postMatch: '' }; return { preMatch: value.slice(0, index), match: value.slice(index, lastIndex), postMatch: value.slice(lastIndex), }; };
Tests:
findMatchGroupsOld
findMatchGroupsOld(b, a)
findMatchGroupsNew
findMatchGroupsNew(a, b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findMatchGroupsOld
findMatchGroupsNew
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findMatchGroupsOld
1561314.6 Ops/sec
findMatchGroupsNew
6144338.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested on MeasureThat.net. **Benchmark Definition** The benchmark definition represents a JavaScript function that performs string matching between two input strings: `searchPhrase` and `valueToSearchOn`. The function, named `findMatchGroupsNew`, takes an additional parameter `locale`. The test case uses the `toLocaleLowerCase` method of JavaScript's String object to convert the search phrase to lowercase. It then uses a regular expression (`RegExp`) to find the match in the value string. **Options Compared** Two approaches are compared: 1. **findMatchGroupsOld**: This approach uses an older implementation that escaped special characters using a custom `escapeMap` and then joined them back together using `join`. The function returns the matched groups as an array. 2. **findMatchGroupsNew**: This approach uses the newer implementation, which directly escapes special characters using the `toLocaleLowerCase` method of the String object. It also uses regular expressions to find the match. **Pros and Cons** ### findMatchGroupsOld Pros: * May be more compatible with older browsers or environments that don't support modern JavaScript features. * Could potentially be faster due to the simplicity of the implementation. Cons: * Uses an outdated `escapeMap` approach, which may lead to security issues if not properly implemented. * Returns matched groups as a raw string, which may not be suitable for all use cases. ### findMatchGroupsNew Pros: * Uses modern JavaScript features and built-in methods, making it more compatible with modern browsers and environments. * Returns matched groups as an array of substrings, which is often a safer and more convenient format. Cons: * May be slower due to the overhead of regular expressions and string manipulation. * Requires support for modern JavaScript features like `toLocaleLowerCase`. **Locale Considerations** The `locale` parameter in both implementations affects the behavior of the function. It ensures that the search phrase is converted to lowercase before matching, which can improve performance by reducing the number of matching opportunities. However, using locale-specific methods like `toLocaleLowerCase` may introduce additional overhead or variations depending on the system's locale settings. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Use a dedicated string matching library**: Libraries like `string-match` or `regex-string-match` provide optimized implementations for string matching tasks. 2. **Implement a custom regex engine**: Writing your own regex engine can be an interesting project, but it requires significant expertise and resources. 3. **Use a compiled language like C++**: If performance is critical, using a compiled language like C++ may be the best option. However, this would require significant modifications to the code. In summary, the `findMatchGroupsNew` implementation provides modern compatibility, safety, and convenience features, but may come at the cost of slightly higher overhead or slower execution compared to the older `findMatchGroupsOld` approach.
Related benchmarks:
Regex vs split/join happily
Asterisk map replace vs regex replace
replace vs split/join
replace vs split/join v2
Regex vs Split and pop
Comments
Confirm delete:
Do you really want to delete benchmark?