Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TextEncoder vs String hash v2
(version: 1)
Test TextEncoder encoding vs simply encoding with a string
Comparing performance of:
Baseline encoder vs baseline hash
Created:
5 years ago
by:
Registered User
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(); var arrayBuffer = new ArrayBuffer(iterations*1000); var buffer = new Uint8Array(arrayBuffer);
Tests:
Baseline encoder
var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; buffer[j] = str.length; var res = encoder.encodeInto(str, buffer.subarray(j+1)); j = j + res.written; }
baseline hash
var stringMap = {}; var counter = 0; function stringToMap(s) { var i = stringMap[s]; if (i === undefined) { stringMap[s] = counter++; } else { return i; } } var j = 0; for (let i = 0; i < iterations; i++) { var str = randomStrings[i % iterations]; var num = stringToMap(str); buffer[j] = num; j = j + 8; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Baseline encoder
baseline hash
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
Baseline encoder
1827.9 Ops/sec
baseline hash
17189.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a benchmark test case for comparing the performance of `TextEncoder` in JavaScript with two alternative approaches: hashing a string using a custom map and simply encoding the string. **Test Case 1: Baseline Encoder** This test case uses the `TextEncoder` to encode the length of a random string into an array buffer. The encoder is used to encode each character of the string, and the written bytes are stored in the buffer. Pros: * This approach tests the performance of `TextEncoder` in encoding strings. * It's a straightforward test case that can be easily implemented. Cons: * May not accurately represent real-world use cases where strings are encoded for storage or transmission. **Test Case 2: Baseline Hash** This test case uses a custom map to hash the length of a random string. The map is used to store the length of each string, and the hashed value is stored in an array buffer. Pros: * This approach tests the performance of hashing strings. * It can be useful for benchmarking algorithms that involve string hashing. Cons: * May not accurately represent real-world use cases where strings are encoded for storage or transmission. * The custom map implementation may introduce additional overhead compared to a simple `TextEncoder` usage. **Other Considerations** * Both test cases use the same array buffer size and random string generation function, which helps ensure that the results are comparable. * However, it's worth noting that using an array buffer for storing the result of encoding or hashing a string may not be the most efficient approach in real-world scenarios. A more common approach would be to store only the encoded or hashed value. **Library: TextEncoder** `TextEncoder` is a built-in JavaScript function that encodes a string into an array of bytes, suitable for storing in a buffer or transmitting over a network. The `encodeInto()` method takes two arguments: the string to encode and the buffer to write the encoded data into. **Special JS Feature/ Syntax: None** There are no special JavaScript features or syntax used in this benchmark test case that would require additional explanation. **Alternatives** Other approaches for testing the performance of encoding strings include: * Using a different encoding scheme, such as base64 or URL-safe base64. * Testing the performance of different string encoding algorithms, such as UTF-8 or UTF-16. * Benchmarking the performance of other libraries or frameworks that provide string encoding capabilities. Overall, this benchmark test case provides a simple and straightforward way to compare the performance of `TextEncoder` with two alternative approaches. However, it's worth noting that real-world use cases may involve additional considerations such as buffer size, data type, and encoding scheme, which are not addressed in this benchmark.
Related benchmarks:
Encode vs Blob
Encode vs Blob on large string
Encode vs Blob v2
Encode vs Blobie
Comments
Confirm delete:
Do you really want to delete benchmark?