Skip to content

byteLength - Get the number of bytes in a UTF-8 encoded String

The byteLength function will return the count of the bytes in a UTF-8 encoded string.

Syntax

byteLength(string: string): number

Arguments

NameTypeRequiredDescription
stringstringtrueThe string whose bytes will be counted

Example - Getting true byte length of UTF-8 String

Consider the following document:

{
    "time_taken_str": "100μs"
}

If we wish to assess the number of bytes in this string, a naive way to do it is to calculate this using length:

create len from time_taken_str.length()

This will create a field len with value 5. This is the number of characters, but it is not the true length of the string in bytes. That is because in UTF-8, the μ symbol requires 2 bytes to store in UTF-8.

Instead, we can use byteLength to calculate the number of bytes it needs to store this string, encoded in UTF-8:

create byte_len from time_taken_str.byteLength()

This will create a field byte_len which has the value 6, representing the four characters that require a single byte, 1,0,0 and s, along with the single character that requires two bytes, μ.