Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TextDecoder
(version: 0)
Comparing performance of:
TextDecoder vs Concat
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ToStringFromUTF16_1 = (function() { var UTF16 = new TextDecoder("utf-16"); return function(bytes, startIndex, length) { startIndex = startIndex | 0; if (typeof length !== "undefined") length += startIndex; return UTF16.decode(bytes.subarray(startIndex, length)); }; })(); var ToStringFromUTF16_2 = (function() { var fromCharCode = String.fromCharCode; return function (bytes, startIndex, length) { startIndex = startIndex | 0; if (typeof length === "undefined") length = bytes.length - startIndex; var s = "", endIndex = startIndex + length, i = startIndex; while (i < endIndex) { var octet = bytes[i], bytesNeeded = 0, codePoint = 0; if (octet <= 0x7F) { bytesNeeded = 0; codePoint = octet & 0xFF; } else if (octet <= 0xDF) { bytesNeeded = 1; codePoint = octet & 0x1F; } else if (octet <= 0xEF) { bytesNeeded = 2; codePoint = octet & 0x0F; } else if (octet <= 0xF4) { bytesNeeded = 3; codePoint = octet & 0x07; } if (endIndex - i - bytesNeeded > 0) { for (var j = 0; j < bytesNeeded; j++) { octet = bytes[i + j + 1]; codePoint = (codePoint << 6) | (octet & 0x3F); } } else { codePoint = 0xFFFD; // REPLACEMENT CHARACTER bytesNeeded = endIndex - i; } //s += String.fromCodePoint(codePoint); //if (!(codePoint < 0x10FFFF && (codePoint>>>0) === codePoint)) // throw RangeError("Invalid code point: " + codePoint); if (codePoint <= 0xFFFF) { // BMP code point s += fromCharCode(codePoint); } else { codePoint -= 0x10000; s += fromCharCode((codePoint >> 10) + 0xD800, // high surrogate (codePoint % 0x400) + 0xDC00); // low surrogate } i += bytesNeeded + 1; } return s; }; })(); var bytes = new Uint8Array([32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0])
Tests:
TextDecoder
ToStringFromUTF16_1(bytes);
Concat
ToStringFromUTF16_2(bytes);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
TextDecoder
Concat
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
TextDecoder
3136647.2 Ops/sec
Concat
5828569.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark for the `TextDecoder` API, which is used to decode bytes from different encoding schemes into a string. **Benchmark Definition** The benchmark definition consists of two scripts: `ToStringFromUTF16_1` and `ToStringFromUTF16_2`. These scripts test two different approaches to decoding UTF-16 encoded strings: 1. **`ToStringFromUTF16_1`**: This script uses the `TextDecoder` API with the `utf-16` encoding scheme. It takes a byte array, a start index, and a length as input and returns the decoded string. 2. **`ToStringFromUTF16_2`**: This script uses a manual decoding approach to convert UTF-16 encoded bytes into a string. It also takes a byte array, a start index, and a length as input. **Options Compared** The benchmark compares two options: 1. Using the `TextDecoder` API (`StringFromUTF16_1`) 2. Manual decoding using the `fromCharCode` method (`StringFromUTF16_2`) **Pros and Cons of Each Approach** **Using the `TextDecoder` API (`StringFromUTF16_1`)** Pros: * Faster execution times due to optimized native implementation * More accurate results as it handles all encoding schemes and edge cases correctly * Easier to maintain and update, as changes are handled by the browser's engine Cons: * May have dependencies on specific browser versions or platforms * Can be slower in older browsers or with limited resources **Manual Decoding (`StringFromUTF16_2`)** Pros: * No dependencies on specific browser versions or platforms * Can be faster for certain input data due to optimized algorithmic performance Cons: * More prone to errors and edge cases, as it relies on manual implementation * May require more maintenance and updates to ensure accuracy and compatibility **Other Considerations** 1. **Browser Support**: The `TextDecoder` API is supported by most modern browsers, but older versions or platforms may not support it. 2. **Encoding Schemes**: The benchmark tests decoding UTF-16 encoded strings, but other encoding schemes (e.g., UTF-8) may require different approaches. 3. **Optimization Techniques**: Browser engines and JavaScript interpreters often apply optimization techniques, such as inlining, caching, or code reordering, which can affect the benchmark results. **Alternatives** Other alternatives for decoding UTF-16 encoded strings include: 1. Using the `Intl` API's `TextDecoder` interface 2. Utilizing third-party libraries or tools for Unicode normalization and encoding conversion These alternatives may offer different trade-offs in terms of performance, accuracy, and maintainability compared to using the `TextDecoder` API or manual decoding approaches. **Special JS Feature** The benchmark uses a special JavaScript feature: **bitwise operations** (e.g., `startIndex | 0;`, `bytesNeeded = endIndex - i;`). These operations are used to perform bitwise arithmetic on integers, which can be beneficial for performance-critical code paths.
Related benchmarks:
TextDecoder 'ascii' vs String.fromCharCode
TextDecoder('utf-16') vs String.fromCharCode
TextDecoder 'ascii' vs String.fromCharCodea
Base64 vs. base64
Comments
Confirm delete:
Do you really want to delete benchmark?