Last active
December 14, 2015 12:38
-
-
Save InPermutation/5087330 to your computer and use it in GitHub Desktop.
Usage: node bisect.js <testfile>.js
<testfile>.js should expect one integer parameter. bisect.js will binary search and find the integer at which <testfile>.js no longer functions. Useful for figuring out exactly how many stack frames are allowable.
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 fork = require('child_process').spawn; | |
var cProcess = 0; | |
bounds(0, 1000); | |
function exec(param){ | |
cProcess++; | |
return fork(process.argv[0], [process.argv[2], param]); | |
} | |
function bounds(min, max) { | |
exec(max).on('exit', function(exitCode) { | |
if(exitCode == 0) { | |
bounds(max, max*2); | |
} else { | |
console.log("Bounds:", min, max); | |
bisect(min, max); | |
} | |
}); | |
} | |
function bisect(min, max) { | |
if(min == max) | |
{ | |
console.log(min); | |
console.log(cProcess, 'processes created'); | |
return; | |
} | |
var sample = Math.floor((min+max)/2); | |
exec(sample).on('exit', function(exitCode) { | |
if(exitCode == 0) { | |
bisect(sample, max); | |
} | |
else { | |
bisect(min, sample); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment