Skip to content

Instantly share code, notes, and snippets.

@silentmatt
Created September 7, 2017 19:16
Show Gist options
  • Save silentmatt/245e874d6525184b410369669c05795e to your computer and use it in GitHub Desktop.
Save silentmatt/245e874d6525184b410369669c05795e to your computer and use it in GitHub Desktop.
Euclidean distance calculations (http://jsbench.github.io/#245e874d6525184b410369669c05795e) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Euclidean distance calculations</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
const dist = (v1, v2) => {
if (v1.length !== v2.length) {
throw new Error("A vector has extra dimensions.")
}
if (v1.length < 1) {
throw new Error("Vectors don't have any dimensions.")
}
var addedSubtracts = 0
for (let i = 0; i < v1.length; i++) {
addedSubtracts += Math.pow(v1[i] - v2[i], 2)
}
return Math.sqrt(addedSubtracts)
}
const euclideanDistance = (v1, v2) => {
if (v1.length !== v2.length) {
throw new Error("A vector has extra dimensions.")
} else if (v1.length < 1) {
throw new Error("Vectors don't have any dimensions.")
} else {
var scale = 0;
var ssq = 1;
for (var i = 0; i < v1.length; i++) {
var x = v1[i] - v2[i];
if (x !== 0) {
var absx = Math.abs(x);
if (scale < absx) {
ssq = 1 + ssq * Math.pow(scale / absx, 2);
scale = absx;
} else {
ssq = ssq + Math.pow(absx / scale, 2);
}
}
}
return scale * Math.sqrt(ssq);
}
}
};
suite.add("Eucli", function () {
// Eucli
dist([0, 0], [0, 0]);
dist([1, 0], [0, 0]);
dist([1], [0]);
dist([0, 0], [10, 10]);
dist([10, 12, 8, 4], [87, 76, 92, 1]);
dist([5, 5, 5], [5, 5, 5]);
dist([0, 0], [3, 4]);
dist([3, 4, 5], [5, 5, 5]);
dist([1, 2, 8, 4], [8, 7, 9, 1]);
});
suite.add("Adapted from LAPACK", function () {
// Adapted from LAPACK
euclideanDistance([0, 0], [0, 0]);
euclideanDistance([1, 0], [0, 0]);
euclideanDistance([1], [0]);
euclideanDistance([0, 0], [10, 10]);
euclideanDistance([10, 12, 8, 4], [87, 76, 92, 1]);
euclideanDistance([5, 5, 5], [5, 5, 5]);
euclideanDistance([0, 0], [3, 4]);
euclideanDistance([3, 4, 5], [5, 5, 5]);
euclideanDistance([1, 2, 8, 4], [8, 7, 9, 1]);
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Euclidean distance calculations");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment