Created
September 22, 2011 08:31
-
-
Save ambar/1234338 to your computer and use it in GitHub Desktop.
detect size of call stack
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 iter = 100 * 100; | |
bm('instanceof',iter,function() { | |
f instanceof Function | |
}) | |
bm('typeof',iter,function() { | |
typeof f === 'function' | |
}) | |
*/ | |
if(!console.time){ | |
var logger = {} | |
var now = function(){ | |
return + new Date(); | |
} | |
console.time = function(name) { | |
logger[name] = now(); | |
} | |
console.timeEnd = function(name) { | |
console.log(name+':', ( now()-logger[name] )+'ms'); | |
delete logger[name]; | |
} | |
} | |
var bm = function(name,iter,action){ | |
console.time(name) | |
while(iter--) action() | |
console.timeEnd(name) | |
} | |
module.exports = bm; |
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 bm = require('./benchmark.js') | |
var Funcf = {}; | |
Funcf.unfold_recursive = function() { | |
var _unfold = function(obj,incrementor,idx) { | |
var next = incrementor(obj,idx++); | |
return [obj].concat( | |
next == null ? [] : _unfold(next,incrementor,idx) | |
) | |
} | |
return function(obj,incrementor) { | |
return _unfold(obj,incrementor,0); | |
} | |
}(); | |
Funcf.unfold_iter_1var = function(obj,incrementor) { | |
var aggr = [obj]; | |
var iter = function(next) { | |
var next = incrementor(next,aggr.length-1); | |
if(next == null) return aggr; | |
aggr.push(next); | |
return iter(next) | |
} | |
return iter(obj); | |
} | |
Funcf.unfold_iter_2var = function(obj,incrementor) { | |
var iter = function(aggr,next) { | |
var next = incrementor(next,aggr.length-1); | |
if(next == null) return aggr; | |
aggr.push(next); | |
return iter(aggr,next) | |
} | |
return iter([obj],obj); | |
} | |
/* anonymous version 3 & 4 */ | |
Funcf.unfold_iter_3var = function(obj,incrementor) { | |
var iter = function(aggr,obj,idx) { | |
var len = aggr.length, next = incrementor(obj,len-1); | |
if(next == null) return aggr; | |
aggr[len] = next; | |
return iter(aggr,next,idx+1) | |
} | |
return iter([obj],obj,0); | |
}; | |
Funcf.unfold_iter_4var = function(obj,incrementor) { | |
var iter = function(aggr,obj,incrementor,idx) { | |
var len = aggr.length, next = incrementor(obj,len-1); | |
if(next == null) return aggr; | |
aggr[len] = next; | |
return iter(aggr,next,incrementor,idx+1) | |
} | |
return iter([obj],obj,incrementor,0); | |
}; | |
/* closure version 3 & 4 */ | |
Funcf.unfold_iter_3var_closure = function() { | |
var iter = function(aggr,obj,incrementor) { | |
var len = aggr.length, next = incrementor(obj,len-1); | |
if(next == null) return aggr; | |
aggr[len] = next; | |
return iter(aggr,next,incrementor) | |
} | |
return function(obj,incrementor) { | |
return iter([obj],obj,incrementor); | |
} | |
}(); | |
Funcf.unfold_iter_4var_closure = function() { | |
var iter = function(aggr,obj,incrementor,idx) { | |
var len = aggr.length, next = incrementor(obj,len-1); | |
if(next == null) return aggr; | |
aggr[len] = next; | |
return iter(aggr,next,incrementor,idx+1) | |
} | |
return function(obj,incrementor) { | |
return iter([obj],obj,incrementor,0); | |
} | |
}(); | |
Funcf.unfold_while = function(obj,incrementor) { | |
var r = obj, ret = [], count = 0; | |
do{ | |
ret.push(r); | |
}while( (r = incrementor(r,count++)) != null ) ; | |
return ret; | |
}; | |
// console.log( Funcf.unfold_iter_3var_closure(1,function(n,i) {if(n!=10) return n + 1}) ); return | |
// ========= PART I ========= | |
var detect_max_iter = function(name) { | |
var max = 1, inc = function(n) { if(n!=max) return n + 1 } | |
try{ | |
while(true){ | |
Funcf[name](0,inc); | |
max += 1; | |
// console.log('max pass',max); | |
} | |
}catch(e){ | |
// e.message => | |
// chrome :'Maximum call stack size exceeded', | |
// firefox : 'too much recursion' | |
console.log('\nmax-iter-count:',max); | |
} | |
} | |
// detect call stack size | |
var testMax = function(name) { | |
bm('time-elapsed:'+name,1,function() { | |
detect_max_iter(name) | |
}); | |
} | |
// WARN : 217912ms | |
// testMax('unfold_recursive') | |
/**/ | |
testMax('unfold_iter_1var'); | |
testMax('unfold_iter_2var'); | |
testMax('unfold_iter_3var'); | |
testMax('unfold_iter_4var'); | |
testMax('unfold_iter_3var_closure'); | |
testMax('unfold_iter_4var_closure'); | |
// ========= PART II ========= | |
var max = 4400; | |
var inc = function(n,i) { | |
// console.log(n,i); | |
if(n!=max) return n + 1 | |
} | |
var iter = 100 * 10; | |
var test = function(name) { | |
bm('test_'+name,iter ,function() { | |
return Funcf[name](1,inc).join() | |
}); | |
} | |
test('unfold_while') | |
test('unfold_recursive') | |
test('unfold_iter_1var') | |
test('unfold_iter_2var') | |
test('unfold_iter_3var') | |
test('unfold_iter_4var') | |
test('unfold_iter_3var_closure') | |
test('unfold_iter_4var_closure') |
Author
ambar
commented
Sep 22, 2011
- PART I:
- PART II:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment