Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
is unique
(version: 0)
Comparing performance of:
Regex vs Non-Regex
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var isUnique = (existingValues) => { return function(value) { value = value.toLowerCase().trim(); const exists = existingValues.find(ev => ev.toLowerCase().trim() === value) !== undefined; return exists ? false : true; }; } var keyLookup = (obj, path, defaultValue) => path.split('.').reduce((a, c) => (a && a[c] ? a[c] : defaultValue || null), obj); var values = { searchBases: [ { id: "", node: "c=1", a: false, b: true }, { id: "", node: "c=2", a: false, b: true }, { id: "", node: "c=3", a: false, b: true }, { id: "", node: "c=4", a: false, b: true }, { id: "", node: "c=5", a: false, b: true }, { id: "", node: "c=6", a: false, b: true }, { a: true, node: "" } ], };
Tests:
Regex
function isUniqueByRef(name) { return function(value, values) { const currentValue = value.toLocaleLowerCase().trim(); // name should be in the form of a.b[x].c // This regex splits the name so we can iterate over the array. // In this case, parent is "a.b" and child and "c" const [parent, child] = name.split(/\[\d+\]\./); // Extract the index from the name. // If name is a.b[x].c then match will be "x" const match = name.match(/[(\d+)]/g); // Convert the value into a number const currentIndex = Number(match); // Get the array from the current form values // Remove the current index from the list of existing values const list = keyLookup(values, parent).filter((item, index) => index !== currentIndex); const existingValues = list.map(item => keyLookup(item, child)).filter(item => item !== null); return isUnique(existingValues)(currentValue); }; } isUniqueByRef("searchBases[0].node")("c=7", values)
Non-Regex
function isUniqueByRef(name) { return function(value, values) { const currentValue = value.toLocaleLowerCase().trim(); const start = name.indexOf('['); const end = name.indexOf(']'); const parent = name.substring(0, start); const child = name.substring(end + 2); const match = name.substring(start + 1, end); // Convert the value into a number const currentIndex = Number(match); // Get the array from the current form values // Remove the current index from the list of existing values const list = keyLookup(values, parent).filter((item, index) => index !== currentIndex); const existingValues = list.map(item => keyLookup(item, child)).filter(item => item !== null); return isUnique(existingValues)(currentValue); }; } isUniqueByRef("searchBases[0].node")("c=7", values)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
Non-Regex
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 benchmark and explore what's being tested. **Benchmark Overview** The benchmark is testing two approaches to determine if a value is unique in an array: 1. **Non-Regex approach**: This approach uses string manipulation to extract the index from the array path. 2. **Regex approach**: This approach uses regular expressions to extract the index from the array path. **Options Compared** We're comparing the performance of two approaches: Non-Regex and Regex. Both approaches aim to determine if a value is unique in an array, but they use different techniques to achieve this. **Non-Regex Approach** The Non-Regex approach uses string manipulation to extract the index from the array path. Here's a high-level overview: * It splits the name by '.' to get the parent and child elements. * It extracts the index from the name using string indexing (e.g., `name.substring(start + 1, end)`). * It converts the extracted value to a number. * It uses the `keyLookup` function to get the array from the current form values and remove the current index. **Regex Approach** The Regex approach uses regular expressions to extract the index from the array path. Here's a high-level overview: * It extracts the start and end indices of the array path using string indexing (e.g., `name.indexOf('[')` and `name.indexOf(']')`). * It extracts the index from the name using a regular expression (e.g., `[\\d+]`). * It converts the extracted value to a number. * It uses the `keyLookup` function to get the array from the current form values and remove the current index. **Pros and Cons** **Non-Regex Approach:** Pros: * Easier to understand and maintain, as it relies on string manipulation rather than regular expressions. * May be faster for simple cases, as it avoids the overhead of regular expression creation. Cons: * Less efficient for more complex cases, as it requires manual indexing and string manipulation. * May not work well with edge cases or non-standard input formats. **Regex Approach:** Pros: * More flexible and efficient for complex cases, as it uses a powerful matching engine to extract the index. * Can handle edge cases and non-standard input formats more robustly. Cons: * More difficult to understand and maintain, as it relies on regular expressions. * May be slower than the Non-Regex approach due to the overhead of regular expression creation. **Other Considerations** Both approaches use the `keyLookup` function to get the array from the current form values. This function is not explicitly defined in the benchmark code, but it's likely a utility function that returns an object with the array at the specified path. The benchmark also uses a predefined `values` object that contains multiple arrays and objects. The test cases use this object to simulate real-world scenarios. **Alternative Approaches** Other possible approaches could include: * Using a library or framework-specific API to achieve the same result (e.g., using jQuery's `.index()` method). * Implementing a custom algorithm that uses a different data structure or technique (e.g., a hash table or a binary search tree). However, these alternatives would likely require significant changes to the benchmark code and might not be as efficient or flexible as the current approach.
Related benchmarks:
assign vs comparea
obj compare 2
undefined vs. typeof vs. in vs. hasOwnProperty 25
Test 03
Comments
Confirm delete:
Do you really want to delete benchmark?