jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
var p1 = [{X:10,Y:20},{X:21,Y:15},{X:1,Y:-20}];
var p2 = [{X:100,Y:200},{X:210,Y:150},{X:10,Y:-200}];
var p3 = [{X:30,Y:11},{X:14,Y:-12},{X:0,Y:19}];
var cnt=10;
var min = -9999;
var max = 9999;
function rand () {
return Math.random() * (max - min) + min;
}
for(var i=0;i<cnt;i++)
{
p1.push({X:rand(),Y:rand()});
p2.push({X:rand(),Y:rand()});
p3.push({X:rand(),Y:rand()});
}
function sqr(x) { return x * x }
function dist2(v, w) { return sqr(v.X - w.X) + sqr(v.Y - w.Y) }
function distToSegmentSquared(p, v, w) {
var l2 = dist2(v, w);
if (l2 == 0) return dist2(p, v);
var t = ((p.X - v.X) * (w.X - v.X) + (p.Y - v.Y) * (w.Y - v.Y)) / l2;
if (t < 0) return dist2(p, v);
if (t > 1) return dist2(p, w);
return dist2(p, { X: v.X + t * (w.X - v.X),
Y: v.Y + t * (w.Y - v.Y) });
}
// square distance from a point to a segment
function getSqSegDist(p, p1, p2) {
var x = p1.X,
y = p1.Y,
dx = p2.X - x,
dy = p2.Y - y;
if (dx !== 0 || dy !== 0) {
var t = ((p.X - x) * dx + (p.Y - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = p2.X;
y = p2.Y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = p.X - x;
dy = p.Y - y;
return dx * dx + dy * dy;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
getSqSegDist |
| ready |
distToSegmentSquared |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.