A few people recommend using
unescape(encodeURIComponent(s)) to encode utf-8 and
decodeURIComponent(escape(s)) to decode, but both
unescape are deprecated. Also this method only produces strings, not Uint8Arrays and doesn't handle the UTF-16 case. Why would you need an array of UTF-8 bytes or UTF-16 characters? Because char and byte arrays can be compared and indexed into more easily. Also files store string data in these formats, especially UTF-8. If only bits of your file are in UTF-8 then you have to convert the string parts piecemeal. There are probably other uses too, or else Uint8Array and Uint16Array wouldn't exist.
Here's my code. For Node.js just trim it to the class definition and add
module.exports=unicode. This way you can test it in the browser easily.