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] = intersect_2d(A.top, A.bottom, B.top, B.bottom); const [left, right] = intersect_2d(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 benchmark and its options. **Benchmark Overview** The benchmark measures the performance of JavaScript code that calculates the intersection and clipping of rectangles in 2D space. It consists of two test cases: 1. "full overlap check" 2. "one-side clipping" Both tests create random rectangles and calculate their intersections or clipping results using different approaches. **Options Compared** In this benchmark, we have three options for calculating rectangle overlaps: A) **Intersecting Rectangles**: This approach uses the `intersect_1d` function to determine if two rectangles intersect. If they do, it returns a non-NaN value indicating the intersection coordinates. B) **Clipping Rectangles**: This approach uses the `clip` function to clip one rectangle against another, effectively removing any overlapping area. It then concatenates the clipped rectangles with their corresponding overlap results. C) **Both**: The third option combines both A and B by first clipping one rectangle against another (A), and then intersecting the clipped result with the other rectangle. **Pros and Cons of Each Approach** * **Intersecting Rectangles (Option A)**: + Pros: Fast, simple to implement, and efficient for small rectangles. + Cons: May be slow for large rectangles due to multiple checks. * **Clipping Rectangles (Option B)**: + Pros: Generally faster than intersecting rectangles, especially for larger rectangles. + Cons: More complex implementation, may not be suitable for all cases (e.g., non-rectangular shapes). * **Both (Option C)**: + Pros: Combines the benefits of both approaches, potentially offering better performance and accuracy. + Cons: Increased complexity, may still be slower than a single optimized approach. **Library Usage** In this benchmark, we see the following library usage: * `Math.random()` is used to generate random rectangle coordinates. No other libraries are explicitly mentioned in the code snippet. However, it's worth noting that some browsers (like Chrome) might use their own optimization techniques or caching mechanisms for certain functions, which could affect performance results. **Benchmark Results** The latest benchmark results show: 1. **Chrome 64**: Fastest execution rate (468984.59375 executions/second) for the "one-side clipping" test case. 2. **Chrome 64**: Slower result (0 executions/second) for the "full overlap check" test case, likely due to the more complex calculations involved. Keep in mind that these results are specific to Chrome 64 on a Mac OS X 10.12.6 device and might not be representative of other browsers or platforms.
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?