Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TextDecoder vs Js
(version: 0)
Comparing performance of:
decodeNative vs decodeCharCode vs decoderJs
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Uint8Array([97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99, 100, 102, 103, 97, 98, 99]) var decoder = new TextDecoder(); function decodeNative(arr) { return decoder.decode(arr); } function decodeCharCode(arr) { return String.fromCharCode.apply(null, 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)
decodeCharCode
decodeCharCode(arr)
decoderJs
decoderJs(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
decodeNative
decodeCharCode
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_3_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/134.0.6998.99 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 134 on iOS 18.3.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
decodeNative
4998054.5 Ops/sec
decodeCharCode
2663638.8 Ops/sec
decoderJs
4699381.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark tests three different approaches to decoding a UTF-8 encoded byte array: 1. `decodeNative(arr)`: Uses the built-in `TextDecoder` API to decode the byte array. 2. `decodeCharCode(arr)`: Manually decodes the byte array using the `String.fromCharCode.apply(null, arr)` method. 3. `decoderJs(buffer)`: A custom JavaScript function that manually decodes the byte array. **Options Compared** The three approaches are compared in terms of execution speed, measured by the number of executions per second. * `decodeNative(arr)` uses the built-in `TextDecoder` API, which is a low-level, browser-specific implementation. * `decodeCharCode(arr)` uses a manual, string-based approach that iterates over the byte array and converts each character to its corresponding Unicode code point using `String.fromCharCode.apply(null, arr)`. * `decoderJs(buffer)`: A custom JavaScript function that manually decodes the byte array using a combination of bitwise operations and conditional statements. **Pros and Cons** Here are some pros and cons for each approach: * `decodeNative(arr)`: + Pros: Fast, low-level implementation, optimized for browser performance. + Cons: May have compatibility issues with older browsers or non-standard implementations. * `decodeCharCode(arr)`: + Pros: Manual, string-based approach can be easily understood and maintained. + Cons: Slower than the built-in `TextDecoder` API, may require more memory access patterns. * `decoderJs(buffer)`: + Pros: Customizable, can be optimized for specific use cases or performance requirements. + Cons: Requires manual implementation of decoding logic, may have compatibility issues with older browsers. **Other Considerations** * The benchmark assumes that the input byte array is a valid UTF-8 encoded string. In practice, this may not always be the case, and additional error handling may be necessary. * The `TextDecoder` API has some limitations, such as requiring a single-byte or multi-byte sequence to complete decoding. If the input byte array contains incomplete sequences, this may impact performance. **Alternatives** Other alternatives for decoding UTF-8 encoded byte arrays include: * Using a library like `utf-8 decode` from the Node.js `util` module. * Implementing a custom decoder using a different language or framework (e.g., C++, Python). * Using a third-party library that provides optimized, browser-agnostic UTF-8 decoding implementations.
Related benchmarks:
TextDecoder 'ascii' vs String.fromCharCode
TextDecoder vs String.fromCharCode 2
TextDecoder 'ascii' vs String.fromCharCodea
TextDecoder vs String.fromCharCode vs String.fromCodePoint
Comments
Confirm delete:
Do you really want to delete benchmark?