If you're going to make contours in the end, you could try and use turf.buffer (https://github.com/Turfjs/turf/tree/master/packages/turf-buffer) or some other vector library, instead of this raster-based algo—although turf.buffer seems may have issues sometimes with "negative distance", so I don't know :)