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 j = 0;
for (let i = 0; i < iterations; i++) {
var str = randomStrings[i % iterations];
new TextEncoder().encode(str).length
}
var j = 0;
for (let i = 0; i < iterations; i++) {
var str = randomStrings[i % iterations];
new Blob([str]).size
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TextEncoder | |
Blob |
Test name | Executions per second |
---|---|
TextEncoder | 728.8 Ops/sec |
Blob | 20.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
The provided JSON represents a benchmark for comparing two approaches: encoding strings using TextEncoder
and creating a blob from a string.
TextEncoder Approach
In this approach, a for
loop is used to iterate over an array of random strings generated by the randomStringGen
function. For each iteration, a new TextEncoder
object is created, and its encode
method is called with one of the random strings as an argument. The length of the resulting encoded string is measured.
Blob Approach
In this approach, a similar for
loop is used to iterate over the same array of random strings. For each iteration, a new blob is created from the current random string using the Blob
constructor with a single-element array containing the string. The size of the resulting blob is then measured.
Comparison and Considerations
The main difference between these two approaches lies in how they handle string encoding and conversion to a binary format.
TextEncoder
API, which is designed for efficient encoding of text data into a binary format. The encoded string can be used directly as needed, without requiring additional conversions.Blob
constructor. While this works, it requires an extra step to measure the size of the resulting blob.Pros and Cons:
TextEncoder
uses optimized algorithms for string encoding.Library and Purpose
In this benchmark, two libraries are used:
TextEncoder
: This is a built-in JavaScript library that provides an efficient way to encode text data into a binary format. Its primary purpose is to handle text encoding for various use cases.Blob
: This is another built-in JavaScript library that represents a binary file-like object. While not primarily designed for string encoding, it can be used to convert strings to a binary format.Special JS Features or Syntax
There are no specific JavaScript features or syntax mentioned in the benchmark.
Alternative Approaches
Other alternatives could include:
TextEncoder
, you could use the Buffer
class to encode and manipulate binary data.Uint8Array
) for more efficient storage and manipulation of binary data.Keep in mind that these alternatives would likely introduce additional complexity and may not be as straightforward to implement as the TextEncoder
approach.