Internally google/re2 library works with UTF-8 binary byte arrays, while JS works with UTF-16. Going back and forth requires re-coding data and recalculating offsets, which are linear operations heavy on memory. To alleviate this problem RE2 (this library) allows to use Buffer instead of a string. But Buffer is a Node-specific object. Sometimes we should interface to libraries that produce different types of objects:
Uint8Array
ArrayBuffer
SharedArrayBuffer
DataView
Returning found strings involves creating new objects and copying data even for buffers. Consider using DataView for that.