Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TextDecoder vs String.fromCharCode/escape/decodeURIComponent
(version: 0)
Comparing performance of:
decodeURIComponent/apply vs decodeURIComponent/spread vs TextDecoder
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
data = new Uint8Array([ 208, 158, 208, 177, 208, 188, 208, 176, 208, 189, 209, 131, 209, 130, 209, 139, 208, 185, 32, 47, 32, 84, 104, 101, 32, 66, 101, 103, 117, 105, 108, 101, 100, 32, 40, 49, 57, 55, 49, 41, 32, 66, 68, 82, 105, 112 ]); utf8decoder = new TextDecoder('utf-8', { fatal: true })
Tests:
decodeURIComponent/apply
const result = decodeURIComponent(escape(String.fromCharCode.apply(0, data)));
decodeURIComponent/spread
const result = decodeURIComponent(escape(String.fromCharCode(...data)));
TextDecoder
const result = utf8decoder.decode(data)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
decodeURIComponent/apply
decodeURIComponent/spread
TextDecoder
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
decodeURIComponent/apply
1088623.8 Ops/sec
decodeURIComponent/spread
497998.2 Ops/sec
TextDecoder
3719512.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The benchmark measures the performance of three different approaches: 1. **String.fromCharCode/escape/decodeURIComponent**: This approach uses the `String.fromCharCode` method, followed by the `escape` function, and finally the `decodeURIComponent` function. 2. **TextDecoder**: This approach uses a `TextDecoder` object with the 'utf-8' encoding scheme to decode the provided binary data. **Options Compared** The benchmark compares the performance of two approaches for decoding a URL-encoded string: * **String.fromCharCode/escape/decodeURIComponent**: This approach involves three steps: creating an array of characters using `String.fromCharCode`, escaping those characters, and then decoding them with `decodeURIComponent`. * **TextDecoder**: This approach uses a single function call to decode the binary data directly. **Pros and Cons** Here are some pros and cons for each approach: * **String.fromCharCode/escape/decodeURIComponent**: * Pros: * Wide browser support * Can handle non-ASCII characters * Cons: * More steps involved, which can increase overhead * May not be as efficient due to the additional function calls * **TextDecoder**: * Pros: * Single-step decoding process reduces overhead * Often more efficient and faster than multiple function calls * Cons: * Less browser support (some older browsers may not have this API) **Library and Purpose** The `TextDecoder` object is a part of the Web APIs, introduced in HTML5. It provides a way to decode binary data into a string, using a specified encoding scheme. In this benchmark, the `TextDecoder` object is used to decode the provided binary data (`data`) directly, which eliminates the need for intermediate steps like escaping and decoding. **Special JS Features or Syntax** There are no specific JavaScript features or syntax that are unique to this benchmark. However, it's worth noting that the `escape` function has been deprecated in favor of the `encodeURIComponent` function in modern browsers. The `TextDecoder` object is a relatively new feature, introduced in HTML5. **Other Alternatives** If you need to decode binary data into a string, other alternatives include: * Using the `Buffer` class (Node.js) or `Uint8Array` (browser-side) * Implementing custom decoding logic using bitwise operations and character encoding tables However, these approaches may not be as efficient or convenient as using the built-in `TextDecoder` object in modern browsers.
Related benchmarks:
TextDecoder 'ascii' vs String.fromCharCode
TextDecoder vs String.fromCharCode better
TextDecoder('utf-16') vs String.fromCharCode
TextDecoder 'ascii' vs String.fromCharCodea
Comments
Confirm delete:
Do you really want to delete benchmark?