Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Decoding ascii: TextDecoder vs Js
(version: 0)
Measure performance of ascii decoding for various string lengths.
Comparing performance of:
decodeNative vs decoderJs
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
// 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; }
Tests:
decodeNative
decodeNative(arr)
decoderJs
decoderJs(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
decodeNative
decoderJs
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.5
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
decodeNative
4839465.0 Ops/sec
decoderJs
36030600.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `decodeNative(arr)`: This function uses the built-in `TextDecoder` API, which is a part of the Web API. 2. `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** 1. **TextDecoder API (`decodeNative(arr)`)**: * Pros: Built-in, widely supported, and optimized for performance. * Cons: Requires a browser to support the Web API, and might not be available in older browsers or environments with limited JavaScript capabilities. 2. **Manual decoding logic (`decoderJs(buffer)`)**: * Pros: Can be implemented on any platform that supports JavaScript, including Node.js and other environments where `TextDecoder` is not available. * Cons: Requires more code and effort to implement correctly, and might be slower due to the overhead of manual decoding logic. **Other Considerations** When using the `TextDecoder` API, it's essential to note that: * The browser must support the Web API for this benchmark. * The input string should be a valid UTF-8 encoded string. 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.
Related benchmarks:
TextDecoder 'ascii' vs String.fromCharCode
Decoding ascii: TextDecoder vs Js 2
Decoding ascii: TextDecoder vs Js 3
TextDecoder 'ascii' vs String.fromCharCodea
Comments
Confirm delete:
Do you really want to delete benchmark?