Can the `StringDecoder`'s internal buffer grow indefinitely and cause a memory leak? Explain why or why not.

Node.js interview question for Advanced practice.

Answer

No, the StringDecoder's internal buffer cannot grow indefinitely and does not cause a memory leak. The buffer's purpose is to store only the trailing bytes from a chunk that form an incomplete character. The size of this buffer is bounded by the maximum number of bytes a single character can occupy in the given encoding, minus one. For example: UTF-8: The longest character is 4 bytes. The internal buffer will never need to hold more than 3 bytes. UTF-16LE: The longest sequence is a 4-byte surrogate pair. The buffer will never need to hold more than 3 bytes (e.g., a high surrogate plus one byte of a low surrogate). When a new chunk is processed by decoder.write(), the old buffer content is used and then replaced by the new trailing incomplete sequence. The buffer size remains very small and does not accumulate over time, thus preventing memory leaks.

Explanation

In UTF-8, the longest valid character sequence is 4 bytes. In UTF-16, it's also 4 bytes (for a surrogate pair).

Related Questions