Skip to content

Instantly share code, notes, and snippets.

@ambar
Created September 22, 2011 08:31
Show Gist options
  • Save ambar/1234338 to your computer and use it in GitHub Desktop.
Save ambar/1234338 to your computer and use it in GitHub Desktop.
detect size of call stack
/*
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;
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')
@ambar
Copy link
Author

ambar commented Sep 22, 2011

  • PART I:
max-iter-count: 9351
time-elapsed:unfold_recursive: 217912ms

max-iter-count: 18702
time-elapsed:unfold_iter_1var: 7390ms

max-iter-count: 16364
time-elapsed:unfold_iter_2var: 4375ms

max-iter-count: 11901
time-elapsed:unfold_iter_3var: 3200ms

max-iter-count: 10909
time-elapsed:unfold_iter_4var: 2626ms

max-iter-count: 10070
time-elapsed:unfold_iter_3var_closure: 2163ms

max-iter-count: 9351
time-elapsed:unfold_iter_4var_closure: 1863ms
  • PART II:
test_unfold_while: 189ms
test_unfold_recursive: 19401ms
test_unfold_iter_1var: 229ms
test_unfold_iter_2var: 232ms
test_unfold_iter_3var: 288ms
test_unfold_iter_4var: 316ms
test_unfold_iter_3var_closure: 286ms
test_unfold_iter_4var_closure: 265ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment