Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find version gaps in version spec strings
(version: 0)
Comparing performance of:
Exhaustive list of version numbers vs Handle only specified ranges
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var versionString = '1,3000000-2000000,2500000,3000000-3500000'; var validInputRegex = /^\s*(\d+(-\d+)?)(,\s*\d+(-\d+)?)*\s*$/;
Tests:
Exhaustive list of version numbers
function getVersionGaps(versions) { if (!versions || !versions.indexOf(',')) { return []; } const parsedVersions = parseVersions(versions); const gaps = []; // If the first version is greater than 1, there is a gap if (parsedVersions[0] > 1) { gaps.push({ start: 1, end: parsedVersions[0] - 1 }); } for (let i = 0; i < parsedVersions.length - 1; i++) { // If the difference between two consecutive versions is greater than 1, there is a gap if (parsedVersions[i + 1] - parsedVersions[i] > 1) { gaps.push({ start: parsedVersions[i] + 1, end: parsedVersions[i + 1] - 1 }); } } return gaps; } function parseVersions(versions) { // Check if the input string matches any of the expected formats if (!versions || versions.trim().length == 0 || versions === 'undefined') { return []; } if (!versions.match(validInputRegex)) { throw new Error(`Invalid input format. Expected format: '3' or '1,2,3' or '1-3'. Received: ${versions}`); } const result = []; const sections = versions.split(','); for (const section of sections) { if (section.includes('-')) { const [start, end] = section.split('-').map(Number); for (let i = start; i <= end; i++) { result.push(i); } } else { result.push(Number(section)); } } return result; } getVersionGaps(versionString);
Handle only specified ranges
function getVersionGaps(versions) { if (!versions || !versions.trim()) return []; const parsedVersions = parseVersionString(versions); return parsedVersions.reduce((gaps, current, index, arr) => { const prevEnd = index === 0 ? 0 : arr[index - 1].end; if (current.start > prevEnd + 1) { gaps.push({ start: prevEnd + 1, end: current.start - 1 }); } return gaps; }, []); } function parseVersionString(versionString) { const ranges = []; if (!versionString || !versionString.trim()) { return ranges; } const parts = versionString.split(','); for (const part of parts) { if (part.includes('-')) { const [start, end] = part.split('-').map(Number); ranges.push({start, end}); } else { const num = Number(part); ranges.push({start: num, end: num}); } } return optimizeRanges(ranges); } function optimizeRanges(ranges) { return ranges .sort((a, b) => a.start - b.start) .reduce((optimized, current) => { const last = optimized[optimized.length - 1]; if (!last || current.start > last.end + 1) { optimized.push(current); } else { last.end = Math.max(last.end, current.end); } return optimized; }, []); } getVersionGaps(versionString);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Exhaustive list of version numbers
Handle only specified ranges
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Exhaustive list of version numbers
523.3 Ops/sec
Handle only specified ranges
1404526.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested. **Benchmark Definition:** The benchmark is testing two approaches to find version gaps in a given string of version numbers. **Approach 1: Exhaustive list of version numbers** In this approach, the `getVersionGaps` function splits the input string into individual version numbers and parses each one. It then checks for gaps between consecutive versions by comparing their differences. The function returns an array of gap objects, where each object contains the start and end of a gap. **Approach 2: Handle only specified ranges** In this approach, the `getVersionGaps` function uses a different parsing logic to extract version numbers from the input string. It splits the string into individual parts and checks if each part includes a hyphen (indicating a range). If so, it extracts the start and end values of the range. The function then optimizes the extracted ranges by merging overlapping ones. **Comparison:** | Approach | Time Complexity | Space Complexity | | --- | --- | --- | | Exhaustive list | O(n^2) due to nested for loops | O(n) for storing parsed versions | | Handle only specified ranges | O(n log n) due to sorting and merging | O(n) for storing extracted ranges | **Pros and Cons:** * **Exhaustive list:** Pros: + Simple to implement + Works well for small inputs Cons: + Time complexity is high, making it slower for large inputs + Uses more memory to store parsed versions * **Handle only specified ranges:** Pros: + Better time complexity, making it faster for large inputs + Reduces memory usage by storing extracted ranges Cons: + More complex implementation + May not work as well for small inputs **Library Used:** The `parseVersions` function uses a regular expression (`validInputRegex`) to validate the input format. The `optimizeRanges` function sorts and merges overlapping ranges. **Special JavaScript Feature/Syntax:** * The ` trim()` method is used to remove whitespace from strings. * The `includes()` method is used to check if a string contains a certain substring. * The `map()` method is used to apply a transformation to an array of values. * The ` reduce()` method is used to accumulate a value by applying a function to each element in an array. **Alternatives:** Other approaches could include: 1. Using a more efficient parsing algorithm for version numbers (e.g., using a trie data structure). 2. Using a precompiled version number parser that can be optimized for the specific use case. 3. Comparing the two approaches on smaller inputs to see if one is significantly faster or more efficient than the other. In conclusion, the benchmark tests the performance of two different approaches to find version gaps in a given string of version numbers. While the "Exhaustive list" approach is simpler to implement but slower for large inputs, the "Handle only specified ranges" approach is faster and more memory-efficient but more complex to implement.
Related benchmarks:
Three Digit Validator
Three Digit Validator 2
Alphanumeric String
Alphanumeric String test 1
Alphanumeric String test 2
Comments
Confirm delete:
Do you really want to delete benchmark?