Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
pointDistance vs rectDistance
(version: 0)
he
Comparing performance of:
pointDistance vs rectDistance
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function pointDistance( fromX, fromY, toX, toY, ) { const diffX = toX - fromX; const diffY = toY - fromY; const distance = Math.sqrt(diffX ** 2 + diffY ** 2); return [distance, diffX / distance, diffY / distance]; } function rectDistance(xA, yA, radiusA, xB, yB, radiusB) { const leftA = xA - radiusA; const rightA = xA + radiusA; const topA = yA - radiusA; const bottomA = yA + radiusA; const leftB = xB - radiusB; const rightB = xB + radiusB; const topB = yB - radiusB; const bottomB = yB + radiusB; const xDistance = leftB > rightA ? leftB - rightA : leftA > rightB ? leftA - rightB : 0; const yDistance = topB > bottomA ? topB - bottomA : topA > bottomB ? topA - bottomB : 0; return xDistance > yDistance ? xDistance : yDistance; }
Tests:
pointDistance
for (let i = 0, len = 1_000_000; i < len; i++) { const d = pointDistance(i + 1.5, i + 2.5, i + 3.5, i + 4.5); }
rectDistance
for (let i = 0, len = 1_000_000; i < len; i++) { const d = rectDistance(i + 1.5, i + 2.5, 4, i + 3.5, i + 4.5, 5); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
pointDistance
rectDistance
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pointDistance
10.5 Ops/sec
rectDistance
10.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON data and explain what's being tested, compared, and the pros/cons of different approaches. **Benchmark Definition** The benchmark definition represents two JavaScript functions: `pointDistance` and `rectDistance`. These functions are used to calculate the distance between two points (or rectangles) in 2D space. * `pointDistance`: takes four arguments (`fromX`, `fromY`, `toX`, `toY`) and returns an array containing the distance, x-coordinate difference, and y-coordinate difference. * `rectDistance`: takes six arguments (`xA`, `yA`, `radiusA`, `xB`, `yB`, `radiusB`) and returns the Euclidean distance between two rectangles. **Options Compared** The benchmark compares the performance of these two functions for calculating distances in different scenarios: 1. **Point-to-point calculation**: `pointDistance` is used to calculate the distance between points (e.g., `(i + 1.5, i + 2.5)`). 2. **Rectangle-rectangle calculation**: `rectDistance` is used to calculate the distance between rectangles (e.g., `rectDistance(i + 1.5, i + 2.5, 4, i + 3.5, i + 4.5, 5)`). **Pros and Cons** * **Point-to-point calculation**: `pointDistance` is likely to be faster because it only needs to calculate the differences in x and y coordinates, which are simple arithmetic operations. This approach may also be more suitable for small, scattered points. * **Rectangle-rectangle calculation**: `rectDistance` is more complex because it needs to consider the boundaries of both rectangles. This approach might be slower due to the additional calculations involved. However, this scenario can be useful for simulations or games where rectangles have fixed positions and sizes. **Library and Special JS Features** Neither function uses any libraries explicitly mentioned in the benchmark definition (e.g., Math.js). However, the use of bitwise operators (`**` for exponentiation) is present in both functions. **Special JS Feature: Exponentiation Operator (`**`)** In JavaScript, the exponentiation operator `**` was introduced in ECMAScript 2015 (ES6). It allows you to raise a number to a power using a simple syntax. This feature can make code more concise and readable, especially for numerical computations. **Other Alternatives** If you were to implement these functions from scratch without using the provided implementations, here are some alternatives: 1. **Use built-in Math libraries**: Instead of implementing custom distance calculations, consider using libraries like Math.js or a built-in `Math.hypot` function (available in modern browsers). 2. **Optimize for specific use cases**: If you know the input ranges and patterns, you can optimize the functions to reduce overhead and improve performance. 3. **Use approximation methods**: For simple cases, approximations like linear interpolation or bounding box calculations might be faster than exact distance calculations. Keep in mind that these alternatives may not necessarily match the provided implementations' performance or accuracy.
Related benchmarks:
The fastest way to find the distance between two points
Distance Calc, pow vs mult
LatLongDistance
Euclidean distance comparison
Comments
Confirm delete:
Do you really want to delete benchmark?