TextEncoder.prototype.encodeInto()

Vous lisez la version anglaise de ce contenu car il n’existe pas encore de traduction dans cette langue. Aidez-nous à traduire cet article !

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The TextEncoder.prototype.encodeInto() method takes a USVString to encode and a destination Uint8Array to put resulting UTF-8 encoded text into, and returns a dictionary object indicating the progress of the encoding. This is potentially more performant than the older encode() method especially when the target buffer is a view into a Wasm heap.

Syntax

b1 = encoder.encodeInto(string, uint8Array);

Parameters

string
Is a USVString containing the text to encode.
uint8Array
Is a Uint8Array object instance to place the resulting UTF-8 encoded text into.

Return value

A TextEncoderEncodeIntoResult dictionary, which contains two members:

read
The number of UTF-16 units of code from the source that has been converted over to UTF-8. This may be less than string.length if uint8Array did not have enough space.
written
The number of bytes modified in the destination Uint8Array. The bytes written are guaranteed to form complete UTF-8 byte sequences.

Buffer Sizing

To convert a JavaScript string s, the output space needed for full conversion is never less than s.length bytes and never greater than s.length * 3 bytes. If the output allocation (typically within Wasm heap) is expected to be short-lived, it makes sense to simply allocate s.length * 3 bytes for the output, in which case the first conversion attempt is guaranteed to convert the whole string.

If the output is expected to be long-lived, it makes sense to compute minimum allocation roundUpToBucketSize(s.length), the maximum allocation size s.length * 3, and to have a chosen (as a tradeoff between memory usage and speed) threshold t such that if roundUpToBucketSize(s.length) + t >= s.length * 3, you simply allocate for s.length * 3. Otherwise, first allocate for roundUpToBucketSize(s.length) and convert. If the read item it the return dictionary is s.length, the conversion is done. If not, reallocate the target buffer to written + (s.length - read) * 3 and then convert the rest by taking a substring of s starting from index read and a subbuffer of the target buffer starting from index written.

Above roundUpToBucketSize() is a function that rounds up to the allocator bucket size. For example, if your Wasm allocator is known to use power-of-two buckets, roundUpToBucketSize() should return the argument if it is a power-of-two or the next power-of-two otherwise. If the behavior of the Wasm allocator is unknown, roundUpToBucketSize() should be an identity function.

If the behavior of your allocator is unknown, you might want to have up to two reallocation steps and make the the first reallocation step multiply the remaining unconverted length by two instead of three. However, in that case, it makes sense not to implement the usual multiplying by two of the already written buffer length, because in such a case if a second reallocation happened, it would always overallocate compared to the original length times three.

The above advice assumes that you don't need to allocate space for a zero terminator. That is, on the Wasm side you are working with Rust strings or a non-zero-terminating C++ class. If you are working with C++ std::string, even though the logical length is shown to you, you need to take the extra terminator byte into account when computing rounding up to allocator bucket size. See the next section about C strings.

No Zero-Termination

If the input string contains the character U+0000 in the input, encodeInto() will write a 0x00 byte in the output. encodeInto() does not write a C-style 0x00 sentinel byte after the logical output.

If your Wasm program uses C strings, it's your responsibility to write the 0x00 sentinel and you can't prevent your Wasm program from seeing a logically truncated string if the JavaScript string contained U+0000.

Examples

<p class="source">This is a sample paragraph.</p>
<p class="result"></p>
const sourcePara = document.querySelector('.source');
const resultPara = document.querySelector('.result');
const string = sourcePara.textContent;

const textEncoder = new TextEncoder();
const utf8 = new Uint8Array(string.length);

let encodedResults = textEncoder.encodeInto(string, utf8);
resultPara.textContent += 'Bytes read: ' + encodedResults.read +
                          ' | Bytes written: ' + encodedResults.written +
                          ' | Encoded result: ' + utf8;

Specifications

Specification Status Comment
Encoding
The definition of 'TextEncoder.encode()' in that specification.
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
encodeInto
Experimental
Chrome Full support 74Edge No support NoFirefox Full support 66IE No support NoOpera No support NoSafari No support NoWebView Android Full support 74Chrome Android Full support 74Firefox Android Full support 66Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

See also