nit: IMO it seems that detecting sampleSize depending on ratio is a good candidate for a separate function\method. 

Also as it's just rounding down to the nearest power of two, we can use  (considering that 8 is a max) something more generic instead of switch, but it's up to you :) 
```
function (x) {
    x = x | (x >> 1);
    x = x | (x >> 2);
    return x - (x >> 1);
}
```