Skip to content

Instantly share code, notes, and snippets.

@InPermutation
Last active December 14, 2015 12:38
Show Gist options
  • Save InPermutation/5087330 to your computer and use it in GitHub Desktop.
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.
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