Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Decoding ascii: TextDecoder vs Js 2
(version: 0)
Measure performance of ascii decoding for various string lengths.
Comparing performance of:
decodeNative vs decoderJs
Created:
4 years ago
by:
Guest
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 = new Uint16Array(n); for (let i = 0; i < n; ++i) { arr[i] = (Math.random() * 0x10000) | 0; } 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:
9 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 138 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
decodeNative
4911787.5 Ops/sec
decoderJs
13661512.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests the performance of decoding ASCII text using two different approaches: 1. **TextDecoder**: This is a built-in JavaScript API for decoding binary data, such as those returned by `fetch()` or WebSockets. It uses a combination of UTF-8 and Unicode escape sequences to represent characters. 2. **Custom implementation (decoderJs)**: This is the custom JavaScript function provided in the benchmark definition that manually decodes ASCII text. The benchmark measures the performance of both approaches for decoding ASCII text with varying string lengths, specifically for multiple values of `n`, where `n` is a multiple of 4. **Options Compared** The two options being compared are: * **TextDecoder**: Uses built-in JavaScript API to decode binary data. * **decoderJs**: Custom implementation of ASCII decoding using bitwise operations and string manipulation. **Pros and Cons** ### TextDecoder Pros: * Fast and efficient, as it leverages the optimized performance of the browser's underlying encoding and decoding mechanisms. * Robust error handling and support for various encoding schemes, including UTF-8 and Unicode escape sequences. Cons: * May have slower performance for very short or specially crafted strings, due to the overhead of the built-in API. * Limited control over the decoding process, which might be desirable in certain edge cases. ### decoderJs Pros: * Provides fine-grained control over the decoding process, allowing for optimization and customization for specific use cases. * Can potentially outperform TextDecoder for short or specially crafted strings. Cons: * Slower performance compared to TextDecoder, as it involves manual string manipulation and bitwise operations. * More error-prone and prone to bugs, due to the lack of built-in support and robustness features. **Library and Purpose** The `TextEncoder` class used in the benchmark preparation code is a related API that encodes binary data into an array of UTF-16 code units. However, it's not directly relevant to this specific benchmark, which focuses on decoding ASCII text. **Special JS Feature or Syntax** This benchmark doesn't require any special JavaScript features or syntax, as both options (TextDecoder and decoderJs) only rely on standard JavaScript APIs and language constructs. **Other Alternatives** For comparison purposes, other alternatives could include: * Using a different encoding scheme, such as UTF-16 or UTF-32. * Employing a custom encoding algorithm, such as base64 or hexadecimal encoding. * Utilizing WebAssembly (WASM) for faster execution of the decoding logic. * Implementing the decoding process using a third-party library or framework. These alternatives would require modifications to the benchmark definition and preparation code, as well as potential changes to the test framework and reporting mechanisms.
Related benchmarks:
TextDecoder 'ascii' vs String.fromCharCode
Decoding ascii: TextDecoder vs Js
Decoding ascii: TextDecoder vs Js 3
TextDecoder 'ascii' vs String.fromCharCodea
Comments
Confirm delete:
Do you really want to delete benchmark?