Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Throughput of rectangle overlap calculator
(version: 0)
Comparing performance of:
full overlap check vs one-side clipping
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var max = Math.max.bind(Math); var min = Math.min.bind(Math); function between(a, x, b) { return a <= x && x <= b; } function intersect_1d(start1, end1, start2, end2) { const c1 = between(start1, start2, end1) ? 1 : 0; const c2 = between(start1, end2, end1) ? 2 : 0; const c3 = between(start2, start1, end2) ? 4 : 0; const c4 = between(start2, end1, end2) ? 8 : 0; const _case = c1 | c2 | c3 | c4; switch (_case) { case 0: return [NaN, NaN]; case 1: return [start2, start2]; case 2: return [end2, end2]; case 3: return [start2, end2]; case 4: return [start1, start1]; case 5: return [min(start1, start2), max(start1, start2)]; case 6: return [min(start1, end2), max(start1, end2)]; case 7: return [min(start1, start2), max(start1, end2)]; case 8: return [end1, end1]; case 9: return [min(end1, start2), max(end1, start2)]; case 10: return [min(end1, end2), max(end1, end2)]; case 11: return [min(end1, start2), max(end1, end2)]; case 12: return [start1, end1]; case 13: return [min(start1, start2), max(end1, start2)]; case 14: return [min(start1, end2), max(end1, end2)]; case 15: return [start1, end2]; default: return [NaN, NaN]; } } function intersect_2d(A, B) { const [top, bottom] = intersection(A.top, A.bottom, B.top, B.bottom); const [left, right] = intersection(A.left, A.right, B.left, B.right); if (isNaN(top) || isNaN(bottom) || isNaN(left) || isNaN(right) || (top === bottom) || (left === right)) { return []; } return [{ left, top, right, bottom }]; } function clip(A, B) { var rectangles = []; if (A.top < B.top) { rectangles.push({ left: A.left, top: A.top, right: A.right, bottom: B.top }); } if (A.left < B.left) { rectangles.push({ left: A.left, top: max(A.top, B.top), right: B.left, bottom: min(A.bottom, B.bottom) }); } if (A.right > B.right) { rectangles.push({ left: B.right, top: max(A.top, B.top), right: A.right, bottom: min(A.bottom, B.bottom) }); } if (A.bottom > B.bottom) { rectangles.push({ left: A.left, top: B.bottom, right: A.right, bottom: A.bottom }); } return rectangles; } function overlap(A, B) { const rA = clip(A, B).map(rect => ({ rect, owner: 1 })); const rB = clip(B, A).map(rect => ({ rect, owner: 2 })); const rAB = intersect_2d(A, B).map(rect => ({ rect, owner: 3 })); return rA.concat(rB).concat(rAB); }
Tests:
full overlap check
overlap({ left: Math.random(), top: Math.random(), right: Math.random(), bottom: Math.random(), }, { left: Math.random(), top: Math.random(), right: Math.random(), bottom: Math.random(), });
one-side clipping
clip({ left: Math.random(), top: Math.random(), right: Math.random(), bottom: Math.random(), }, { left: Math.random(), top: Math.random(), right: Math.random(), bottom: Math.random(), });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
full overlap check
one-side clipping
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 provided benchmark definition JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition:** The benchmark definition is a JavaScript code that calculates the intersection of two rectangles (clip) and checks for full overlap between them (overlap). The `clip` function takes two rectangles as input and returns an array of clipped rectangles. The `overlap` function takes two rectangles as input, applies the `clip` function to each rectangle from both perspectives (i.e., `A` clipping `B` and `B` clipping `A`), and then finds the intersection of these clipped rectangles. **Options Compared:** The benchmark compares two options: 1. **Full Overlap Check**: The `overlap` function checks if the entire area of one rectangle overlaps with another rectangle. 2. **One-Side Clipping**: The `clip` function applies clipping to one side of a rectangle, effectively removing that side from the overlap check. **Pros and Cons:** **Full Overlap Check (Option 1)** Pros: * More accurate representation of real-world overlap scenarios * Can be more efficient for certain use cases where partial overlaps are not relevant Cons: * May be computationally expensive due to the complexity of finding intersections * Requires additional logic to handle edge cases and invalid input **One-Side Clipping (Option 2)** Pros: * Simplifies the overlap check by removing one side of the rectangle from consideration * Can be faster than the full overlap check, especially for smaller rectangles or those with limited overlap areas Cons: * May not accurately represent real-world scenarios where overlapping occurs on multiple sides * Requires additional logic to handle edge cases and invalid input **Library:** The `intersection` function in the `overlap` code appears to be a custom implementation of the intersection algorithm, which is used to find the common area between two rectangles. **Device Platforms:** The benchmark results are specific to Chrome 64 on Mac OS X 10.12.6. The results indicate that the one-side clipping option is faster than the full overlap check, with approximately 465,947 executions per second compared to 0 executions per second for the full overlap check. Keep in mind that these results may not be representative of other devices or browsers.
Related benchmarks:
Math.max/min vs if vs ternary vs bitwise & ~~ - 4 numbers
Math.max/min vs if vs ternary vs bitwise & ~~ & lodash - 5 numbers
min and max
Random Integer Generator (favors numbers closer to 0)
Triangular RV
Comments
Confirm delete:
Do you really want to delete benchmark?