Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare TextEncoder, Blob, new TextEncoder
(version: 0)
Comparing performance of:
TextEncoder vs Blob vs TextEncoder new
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
iterations = 1000; function randomStringGen(n) { let strings = []; for (let i = 0; i < n; i++) { let s = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); strings[i] = s; } return strings; } var randomStrings = randomStringGen(iterations); var encoder = new TextEncoder();
Tests:
TextEncoder
var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; console.log(encoder.encode(str).length) }
Blob
var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; console.log(new Blob([str]).size) }
TextEncoder new
var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; console.log(new TextEncoder().encode(str).length) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
TextEncoder
Blob
TextEncoder new
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
TextEncoder
389.2 Ops/sec
Blob
19.0 Ops/sec
TextEncoder new
360.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net provides a platform for JavaScript microbenchmarks, allowing users to compare the performance of different approaches and libraries in various scenarios. The provided benchmark measures the encoding speed of three methods: 1. **TextEncoder**: The built-in `TextEncoder` API in modern browsers. 2. **Blob**: Creating a new `Blob` object from an encoded string using the `new Blob([str])` syntax. 3. **new TextEncoder**: A custom instance of the `TextEncoder` class created with the `new` keyword. **Benchmark Preparation Code** The benchmark preparation code consists of two parts: * **Script Preparation Code**: * It defines a function `randomStringGen(n)` that generates an array of random strings. * The length of each string is fixed at 15 characters, and the same pattern is repeated for the specified number of iterations (`iterations`). * **Html Preparation Code**: This section is empty, indicating that no specific HTML setup or configuration is required. **Individual Test Cases** The benchmark consists of three test cases: 1. **TextEncoder** * The script encodes each random string using `encoder.encode(str)` and measures the resulting byte length. 2. **Blob** * The script creates a new `Blob` object from an encoded string using `new Blob([str])` and measures its size. 3. **TextEncoder new** * This test case is similar to the first one, but it uses a custom instance of `TextEncoder` created with the `new` keyword. **Library and Customization** The benchmark tests the performance of three approaches: * **TextEncoder**: The built-in API in modern browsers. * **Blob**: Creating a new `Blob` object from an encoded string. * **new TextEncoder**: A custom instance of the `TextEncoder` class created with the `new` keyword. **Special Considerations and Features** There is no specific JavaScript feature or syntax mentioned in the benchmark code. However, note that some browsers may have variations in their implementation of these APIs, which could affect performance. **Alternatives and Considerations** When working with text encoding and blob creation, consider the following alternatives: * **Binary Encoding**: Instead of using `TextEncoder`, you can use binary encoding methods like `Uint8Array` or `Buffer`. * **Other Blob Creation Methods**: While creating a new `Blob` object from an encoded string is used in this benchmark, there are other methods to create blobs, such as using a binary data URL or the `File` API. * **Optimization Techniques**: Depending on your use case, you may be able to optimize your code further by reducing unnecessary encodings or using more efficient data structures. By understanding these factors and alternatives, you can make informed decisions when working with text encoding and blob creation in your JavaScript applications.
Related benchmarks:
Encode vs Blob
Encode vs Blob on large string
Blob vs TextEncoder
Encode vs Blob v2
Encode vs Blob v2.1
Comments
Confirm delete:
Do you really want to delete benchmark?