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
}
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 | |
Buffer.byteLength |
Test name | Executions per second |
---|---|
TextEncoder | 832.6 Ops/sec |
Blob | 12.8 Ops/sec |
Buffer.byteLength | 11.8 Ops/sec |
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:
TextEncoder
Blob
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:
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.
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.
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
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
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
Alternatives
There are several alternatives for measuring the size of strings in JavaScript:
length
property: Using the length
property directly on a string object can provide a quick estimate of its length.String.prototype.length
: Using the length
property on a string object is similar to using the length
property directly on the string object.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.