TextEncoder.prototype.encodeInto() メソッドは、USVStringを使用してエンコードし、宛先の Uint8Array を使用して結果の UTF-8 エンコードされたテキストを挿入し、エンコードの進行状況を示す辞書オブジェクトを返します。
これは、特にターゲット バッファが Wasm ヒープへのビューである場合に、古い encode() メソッドよりもパフォーマンスが向上する可能性があります。
encode.encodeInto は、常にその出力を配列の先頭に置きます。ただし、出力を特定のインデックスから開始すると便利な場合があります。解決策は TypedArray.prototype.subarray() です。
var encoder = new TextEncoder;
function encodeIntoAtPosition(string, u8array, position) {
return encoder.encodeInto(string, position ? u8array.subarray(position|0) : u8array);
}
var u8array = new Uint8Array(8);
encodeIntoAtPosition("hello", u8array, 2);
console.log( "" + u8array.join() ); // 0,0,104,101,108,108,111,0