Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Encode vs Blob v2
(version: 0)
Comparing performance of:
TextEncoder vs Blob vs Buffer.byteLength
Created:
2 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]; new TextEncoder().encode(str).length }
Blob
var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; new Blob([str]).size }
Buffer.byteLength
var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; new Blob([str]).size }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
TextEncoder
Blob
Buffer.byteLength
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
TextEncoder
889.7 Ops/sec
Blob
9.8 Ops/sec
Buffer.byteLength
9.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark is called "Encode vs Blob v2". It tests the performance of three different approaches to measure the size of a string: 1. `TextEncoder` 2. `Blob` 3. `Buffer.byteLength` **Script Preparation Code** The script preparation code generates an array of random strings using the `randomStringGen` function, which creates strings by concatenating two random hexadecimal strings. This function is used to generate input data for the benchmark. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** There are three test cases: 1. **TextEncoder** ```javascript var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; new TextEncoder().encode(str).length } ``` This test case uses the `TextEncoder` API to measure the size of each input string. 2. **Blob** ```javascript var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; new Blob([str]).size } ``` This test case uses the `Blob` constructor to create a blob from each input string and measures its size. 3. **Buffer.byteLength** ```javascript var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; new Blob([str]).size } ``` This test case is actually identical to the `Blob` test case, as it also uses the `Blob` constructor and measures its size. It appears that there was a mistake in the original benchmark definition. **Library and Purpose** * **TextEncoder**: The `TextEncoder` API provides a way to efficiently encode strings into an array of bytes. In this benchmark, it's used to measure the size of each input string. * **Blob**: The `Blob` constructor creates a blob from a sequence of byte-like values (such as strings). In this benchmark, it's used to create blobs from the input strings and measure their size. **Special JS Features** There are no special JavaScript features or syntax mentioned in the benchmark definition. It only uses standard JavaScript APIs and functions. **Other Considerations** * The benchmark uses a small number of iterations (1000) to generate random strings, which may not be representative of real-world usage. * The benchmark is running on a mobile device with Safari 18, which may have different performance characteristics compared to other browsers or devices. **Alternatives** There are several alternatives for measuring the size of strings in JavaScript: 1. **`length` property**: Using the `length` property directly on a string object can provide a quick estimate of its length. 2. **`String.prototype.length`**: Using the `length` property on a string object is similar to using the `length` property directly on the string object. 3. **`Buffer.byteLength()`**: This function, also mentioned in the benchmark definition, provides an alternative way to measure the size of a string by creating a buffer and setting its length. Keep in mind that these alternatives may not be as efficient or accurate as using the `TextEncoder` API or the `Blob` constructor.
Related benchmarks:
Encode vs Blob
Encode vs Blob vs length
Encode vs Blob on large string
Encode vs Blob v2.1
Comments
Confirm delete:
Do you really want to delete benchmark?