This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Benchmarking function (array of functions, iterations, array of arguments) | |
function benchmark(func,n,args){ | |
// analyze arguments passed: empty or num/string or array | |
if (typeof args ==='undefined'){ | |
args=[]; | |
}else if(typeof args[0]==='undefined' || typeof args==='string'){ | |
args=[args]; | |
} | |
// analyze functions | |
if (typeof func==="function") {func=[func]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// deep copy: | |
// String, Number, undefined, null, Set, Map, typed Array, Object, Boolean, RegExp, Date, ArrayBuffer, Node | |
// Functions, Properties of types: (Primitive, Symbol) | |
// shallow copy (by reference): | |
// WeakMap, WeakSet, Symbol | |
// increased compatibility: IE, Node | |
var $jscomp = $jscomp || {}; | |
$jscomp.scope = {}; | |
$jscomp.ASSUME_ES5 = !1; | |
$jscomp.defineProperty = $jscomp.ASSUME_ES5 || "function" == typeof Object.defineProperties ? Object.defineProperty : function(a, b, c) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
firstNotRepeatingCharacter = s => { | |
r = {} | |
for (e of s) | |
// ~n = -(n+1) | |
// -~n = n+1 | |
// ~ undefined = -1 | |
// -~undefined = 1 | |
// if character is in hash increment else set to 1 | |
r[e] = -~r[e] | |
for (e in r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fast random number generator with better distribution | |
function Mash() { | |
var n = 0xefc8249d; | |
var mash = function(data) { | |
data = data.toString(); | |
for (var i = 0; i < data.length; i++) { | |
n += data.charCodeAt(i); | |
var h = 0.02519603282416938 * n; | |
n = h >>> 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fast array sort | |
function heap3Sort() { | |
function swap(ary, a, b) { | |
var t = ary[a]; | |
ary[a] = ary[b]; | |
ary[b] = t; | |
} | |
function shiftDown(ary, start, end) { | |
var root = start, | |
child, s, root21; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check for existence of property in deeply nested object | |
function get_if_exist(str) { | |
try { return eval(str) } | |
catch(e) { return undefined } | |
} | |
//slightly slower | |
function objHasKeys(obj, keys) { | |
var next = keys.shift(); | |
return obj[next] && (! keys.length || objHasKeys(obj[next], keys)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrayHistogram(arr){ | |
const obj={}; | |
for(let i=0;i<arr.length;i++){ | |
if(!obj.hasOwnProperty(arr[i])) obj[arr[i]]=0; | |
obj[arr[i]]++; | |
} | |
return obj; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const quickSort = (function() { | |
var i = 0; | |
function swap(first, second, array) { | |
var temp = array[first]; | |
array[first] = array[second]; | |
array[second] = temp; | |
} | |
return function(array) { | |
for (var len = array.length - 1; i < len; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCompare(o1, o2) { | |
if (typeof o1 !== "object" || o1 === null) return o1 === o2; // fast obj/null test | |
let n; | |
if (o1 instanceof Array) { // fast array test | |
const l = o1.length; | |
for (let i = 0; i < l; i++) { | |
if (deepCompare(o1[i], o2[i])) {} else { | |
return false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kryoPrefetch = { | |
_init: function() { | |
debug = true; | |
store = { | |
clicked: undefined, | |
mouseDown: false, | |
targetMatch: false, | |
pending: false, | |
href: "" | |
}; |
OlderNewer