JavaScript implementation of perimeter estimation algorithm