Created
October 15, 2012 04:11
-
-
Save wwalser/3890784 to your computer and use it in GitHub Desktop.
plane loops
This file contains hidden or 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
//I wrote this on a plane from LA to NC because I had no internet connection and was bored | |
//It's not useful | |
if (typeof window === 'undefined') { | |
var window = global; | |
} | |
function each(array, fun){ | |
var length = array.length, | |
i = 0; | |
for (; i < length; i++){ | |
var current = array[i]; | |
fun.call(current, current, i, array); | |
} | |
} | |
function map(array, fun){ | |
var newArray = []; | |
each(array, function(current, i, array){ | |
newArray.push(fun.call(current, current, i, array)); | |
}); | |
return newArray; | |
} | |
function filter(array, fun){ | |
var newArray = [], | |
mine = function(current, i, array){ | |
var result = fun.call(current, current, i, array); | |
if (result) { | |
newArray.push(current); | |
} | |
} | |
each(array, mine); | |
return newArray; | |
} | |
function reduce(array, fun, memo){ | |
each(array, function(current, i, array){ | |
memo = fun.call(current, current, memo, memo); | |
}); | |
return memo; | |
} | |
function defined(thing){ | |
var current = this; | |
if (typeof thing === 'string'){ | |
if (thing.indexOf('.') !== -1) { | |
thing = thing.split('.'); | |
for (var i = 0; i < thing.length; i++){ | |
if (typeof current[thing[i]] !== 'undefined') { | |
current = current[thing[i]]; | |
} else { | |
return false; | |
} | |
} | |
return current; | |
} else { | |
current = current[thing]; | |
return typeof current === 'undefined' ? false : current; | |
} | |
} else if (typeof thing !== 'undefined'){ | |
return thing; | |
} | |
} | |
function createContinuation(){ | |
var recurseMethod = Array.prototype.shift.call(arguments), | |
args = arguments, | |
count = 0, | |
continuation = function(){ | |
count++; | |
recurseMethod.apply(window, args); | |
} | |
Array.prototype.unshift.call(args, continuation); | |
continuation(); | |
return function(){ | |
return count; | |
} | |
} | |
var timeOutCalls = [ | |
['setTimeout', 0], | |
['setImmediate'], | |
['process.nextTick'] | |
]; | |
var timeoutCounts = map(filter(map(timeOutCalls, function(current){ | |
return reduce(current, function(current, prev){ | |
if (prev === false) { | |
return false; | |
} else { | |
current = defined(current); | |
prev.push(current) | |
return current === false ? false : prev; | |
} | |
}, []); | |
}), function(current){ | |
return current; | |
}), function(current){ | |
return createContinuation.apply(window, current); | |
}); | |
setInterval(function(){ | |
each(timeoutCounts, function(current){ | |
console.log(current()); | |
}); | |
console.log('\n'); | |
}, 5000); | |
//var timeoutCount = createContinuation(setTimeout, 0); | |
//var immediateCount = createContinuation(setImmediate); | |
// setInterval(function(){ | |
// console.log('timeout: ' + timeoutCount()); | |
// console.log('immediate: ' + immediateCount()); | |
// console.log('delta: ' + (immediateCount() - timeoutCount())); | |
// console.log('percentage: ' + (timeoutCount()/immediateCount())*100); | |
// }, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment