Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
@AutoSponge
AutoSponge / args1.js
Created October 27, 2011 01:12
Arguments Speed Boost
//don't slice arguments just to iterate
//http://jsperf.com/handle-args
(function () {
var i, len;
for (i = 0, len = arguments.length; i < len; i += 1) {
console.log(arguments[i], i);
}
}(3, 2, 1));
//don't prepend arguments
@AutoSponge
AutoSponge / util.js
Created April 13, 2012 19:13 — forked from pamelafox/util.js
JavaScript Utility Libraries (Scroll down!)
var ED = ED || {};
// Utility functions
ED.util = (function() {
// Data structure functions
function each(object, callback) {
if (object === null) return;
@AutoSponge
AutoSponge / sumArgs.js
Created September 18, 2012 23:37
sumArgs
function getPrecision(num) {
var split = (num + "").split(/([\.e\-]{1,2})/);
var i, len, token, next, nextNum, precision;
for (precision = 0, i = 1, len = split.length; i < len; i += 1) {
token = split[i];
next = split[i + 1];
nextNum = +next;
if (token === ".") {
precision += next.length;
} else if (token === "e-") {
@AutoSponge
AutoSponge / get.js
Last active October 10, 2015 22:17
simple get
/**
* null-safe retrieval of objects and their property values from context
* @function get
* @param {String|String[]} path
* @param {Object} [obj=this]
* @return
*/
(function (HEAD) {
var depthCache = {};
var cache = {};
@AutoSponge
AutoSponge / anti-loop.js
Last active October 12, 2015 20:18
an each implementation that caches unraveled loops
var each = (function () {
function each(arr, fn) {
var len = arr.length;
return (each[len] || (each[len] = each.factory(len), each[len]))(arr, fn);
}
each.factory = function (len) {
var body = "\tvar i = 0;\n";
while (len--) {
body += "\tfn(i, arr[i++]);\n";
}
@AutoSponge
AutoSponge / array_merge.js
Created November 16, 2012 18:11
merge arrays
Array.prototype.merge = function (/*Array1, ..., ArrayN*/) {
this.push.apply(this, arguments.length > 1 ? arr.concat.apply([], arguments) : arguments[0]);
return this;
};
@AutoSponge
AutoSponge / extend_inherit.js
Created November 28, 2012 19:38
Inheritance pattern
(function (namespace) {
"use strict";
function Class() {}
Class.prototype = {
constructor: Class
};
Class.extend = function (fn) {
if (!fn.name) {
@AutoSponge
AutoSponge / aop.js
Created December 7, 2012 20:57
AOP with callbacks
function createAspect(fn) {
var _1 = jQuery.Callbacks(),
_2 = jQuery.Callbacks(),
_3 = jQuery.Callbacks(),
args, result;
function aspect() {
args = arguments;
_2.fireWith(this, args);
return result;
@AutoSponge
AutoSponge / learning.js
Created December 9, 2012 15:54
learning js
//http://stackoverflow.com/a/2687693/450808
//http://dochub.io
//https://github.com/pamelafox/teaching-materials
//http://www.codecademy.com/tracks/javascript-combined
//http://stackoverflow.com/questions/11246/best-resources-to-learn-javascript
//http://bonsaiden.github.com/JavaScript-Garden/
//http://learn.appendto.com/
//http://net.tutsplus.com/tutorials/javascript-ajax/the-best-way-to-learn-javascript/
//http://rmurphey.com/blog/2010/09/15/in-search-of-javascript-developers-a-gist/
@AutoSponge
AutoSponge / plinko.js
Last active October 13, 2015 22:57
another idea for flow control
function Plinko() {
this.tree = {};
}
Plinko.prototype.add = function (from, fn) {
var leaf,
callback;
if (typeof from === "string") {
leaf = from;
callback = fn;
} else {