Skip to content

Instantly share code, notes, and snippets.

// 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]}
@c7x43t
c7x43t / deepCopy.js
Last active May 25, 2020 19:16
Deep Copy an object very fast
// 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) {
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)
// 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;
// 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;
@c7x43t
c7x43t / objHasKeys.js
Last active March 5, 2018 09:34
ObjectHasKeys - Check for existence of property in deeply nested object
// 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));
}
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;
}
@c7x43t
c7x43t / quickSort.js
Last active February 14, 2018 14:57
Very fast sorting for Javascript - inlined swap is a bit faster
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++) {
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
}
}
var kryoPrefetch = {
_init: function() {
debug = true;
store = {
clicked: undefined,
mouseDown: false,
targetMatch: false,
pending: false,
href: ""
};