Skip to content

Instantly share code, notes, and snippets.

View silentmatt's full-sized avatar

Matthew Crumley silentmatt

View GitHub Profile
@silentmatt
silentmatt / index.html
Created September 7, 2017 19:16
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>
<link rel="import" href="../core-scaffold/core-scaffold.html">
<link rel="import" href="../core-header-panel/core-header-panel.html">
<link rel="import" href="../core-menu/core-menu.html">
<link rel="import" href="../core-item/core-item.html">
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-menu/core-submenu.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-button/paper-button.html">
@silentmatt
silentmatt / make.js
Created November 26, 2010 17:32
Makes a DOM tree from JavaScript "S-expressions"
var make = (function() {
if (typeof Array.isArray !== 'function') {
Array.isArray = function(a) {
return Object.prototype.toString.call(a) === "[object Array]";
};
}
function make(desc) {
if (!Array.isArray(desc)) {
return make.call(this, Array.prototype.slice.call(arguments));
@silentmatt
silentmatt / gist:305288
Created February 16, 2010 04:30
Replaces isNaN function
var a = NaN;
isNaN(a); // true because a has the value NaN
isNaN = function(x) { return x < 5; };
isNaN(a); // false
@silentmatt
silentmatt / approximatelyEqual.js
Created February 14, 2010 19:12
Test two numbers for approximate equality.
function approximatelyEqual(a, b, tolerance) {
return Math.abs(a - b) < tolerance;
}
approximatelyEqual(0.1 + 0.2, 0.3, 1e-12); // true
@silentmatt
silentmatt / RegExp.escape.js
Created January 14, 2010 19:08
Escape special characters in a regular expression
RegExp.escape = (function() {
var special = /([.*+?^${}()|[\]\/\\])/g;
return function escape(s) {
return s.replace(special, '\\$1');
};
})();
@silentmatt
silentmatt / graycode.js
Created June 26, 2009 17:10
JavaScript functions to convert to/from binary-reflected Gray codes
Number.toGrayCode = function(n) {
if (n < 0) {
throw new RangeError("cannot convert negative numbers to gray code");
}
return n ^ (n >>> 1);
};
Number.fromGrayCode = function(gn) {
if (gn < 0) {
throw new RangeError("gray code numbers cannot be negative");
@silentmatt
silentmatt / parameters.js
Created June 19, 2009 18:43
Functions for dealing with function parameters
/*
Get an array of parameter names for a function.
Example:
js> function f(a, b, c) { return a + b * c; }
js> JSON.stringify(Function.parameters(f));
["a","b","c"]
*/
Function.parameters = function(f) {
@silentmatt
silentmatt / name.js
Created June 17, 2009 18:20
JavaScript class to represent and format names
var Name = (function() {
function Name(prefix, first, middle, last, suffix, called) {
this.prefix = prefix || "";
this.first = first || "";
this.middle = middle || "";
this.last = last || "";
this.suffix = suffix || "";
this.called = called || "";
}
@silentmatt
silentmatt / createArray.js
Created June 17, 2009 18:09
easily create multidimensional arrays in JavaScript
function createArray(length) {
var a = new Array(Number(length) || 0);
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < length; i++) {
a[i] = createArray.apply(this, args);
}
}