// needed to create bytes array
var encoder = new TextEncoder();
// string length, multiple of 4 - vary to get different results
var n = 8;
var arr = encoder.encode(Array((n / 4) | 0).fill('abcd'));
var decoder = new TextDecoder();
function decodeNative(arr) {
return decoder.decode(arr);
}
function decoderJs(buffer) {
var start = 0;
var end = buffer.byteLength;
if (end - start < 1) {
return "";
}
var str = "";
for (var i = start; i < end;) {
var t = buffer[i++];
if (t <= 0x7F) {
str += String.fromCharCode(t);
} else if (t >= 0xC0 && t < 0xE0) {
str += String.fromCharCode((t & 0x1F) << 6 | buffer[i++] & 0x3F);
} else if (t >= 0xE0 && t < 0xF0) {
str += String.fromCharCode((t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F);
} else if (t >= 0xF0) {
var t2 = ((t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F) - 0x10000;
str += String.fromCharCode(0xD800 + (t2 >> 10));
str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
}
}
return str;
}
decodeNative(arr)
decoderJs(arr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
decodeNative | |
decoderJs |
Test name | Executions per second |
---|---|
decodeNative | 6869118.5 Ops/sec |
decoderJs | 4343495.5 Ops/sec |
Let's break down what's being tested in the provided JSON benchmark.
Benchmark Definition
The main goal of this benchmark is to compare the performance of two different approaches for decoding ASCII text using JavaScript:
decodeNative(arr)
: This function uses the built-in TextDecoder
API, which is a part of the Web API.decoderJs(buffer)
: This function implements manual decoding logic for UTF-8 encoded strings.Options Compared
The benchmark compares the performance of these two approaches with varying string lengths. The tests are designed to measure the execution time (in executions per second) for each approach as the input string length increases.
Pros and Cons
decodeNative(arr)
):decoderJs(buffer)
):TextDecoder
is not available.Other Considerations
When using the TextDecoder
API, it's essential to note that:
For the manual decoding logic (decoderJs(buffer)
), make sure to handle edge cases and encoding errors correctly.
Library: TextEncoder
The TextEncoder
library is used to create a buffer from a string, which is then passed as an argument to the decodeNative(arr)
function. This buffer is expected to contain the encoded data in UTF-8 format.
JavaScript Features/Syntax
This benchmark does not use any special JavaScript features or syntax that are specific to certain browsers or environments. However, it relies on modern JavaScript features like const
, let
, and arrow functions, which might not be supported in older browsers or environments with limited JavaScript capabilities.
In summary, this benchmark tests the performance of two approaches for decoding ASCII text: using the built-in TextDecoder
API versus implementing manual decoding logic. The results provide insight into the execution time of each approach under various conditions, helping developers optimize their code for better performance.