this *asBuffer* complexity is probably a good case for pulling out a utility function to clean up the code here and give v8 something small to optimise