var uint8Array = {
"0": 146,
"1": 131,
"2": 172,
"3": 109,
"4": 97,
"5": 116,
"6": 101,
"7": 114,
"8": 105,
"9": 97,
"10": 108,
"11": 84,
"12": 121,
"13": 112,
"14": 101,
"15": 168,
"16": 82,
"17": 69,
"18": 83,
"19": 79,
"20": 85,
"21": 82,
"22": 67,
"23": 69,
"24": 162,
"25": 105,
"26": 100,
"27": 161,
"28": 49,
"29": 166,
"30": 97,
"31": 109,
"32": 111,
"33": 117,
"34": 110,
"35": 116,
"36": 164,
"37": 49,
"38": 48,
"39": 48,
"40": 48,
"41": 131,
"42": 172,
"43": 109,
"44": 97,
"45": 116,
"46": 101,
"47": 114,
"48": 105,
"49": 97,
"50": 108,
"51": 84,
"52": 121,
"53": 112,
"54": 101,
"55": 168,
"56": 82,
"57": 69,
"58": 83,
"59": 79,
"60": 85,
"61": 82,
"62": 67,
"63": 69,
"64": 162,
"65": 105,
"66": 100,
"67": 161,
"68": 50,
"69": 166,
"70": 97,
"71": 109,
"72": 111,
"73": 117,
"74": 110,
"75": 116,
"76": 164,
"77": 49,
"78": 48,
"79": 48,
"80": 48
}
uint8Array.toString()
JSON.stringify(uint8Array);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
toString | |
JSON.stringify |
Test name | Executions per second |
---|---|
toString | 109406344.0 Ops/sec |
JSON.stringify | 361988.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark measures the performance of two different approaches to convert a uint8Array
to a string:
uint8Array.toString()
JSON.stringify(uint8Array)
These two functions are used to serialize the array, which is a process of converting it into a string representation that can be stored or transmitted.
Options compared
The benchmark compares the performance of two options:
toString()
: This method converts each element of the array to its ASCII character representation and joins them together with empty strings in between.JSON.stringify()
: This method serializes the array using a JSON string format, which is a more structured and human-readable way to represent data.Pros and Cons
toString()
:
Pros:
Cons:
JSON.stringify()
:
Pros:
Cons:
toString()
Library
In this benchmark, the JSON
library is used, which provides a way to serialize data in a standardized format. The JSON.stringify()
method uses this library to create a JSON string representation of the array.
Special JS feature
There is no special JS feature or syntax explicitly mentioned in this benchmark. However, it's worth noting that JavaScript has various features and methods for serializing data, such as Buffer
objects, which can be used to convert binary data into a string format.
Alternatives
Other alternatives for serializing arrays in JavaScript include:
Array.prototype.join()
: Similar to toString()
, but uses an empty string separator instead.Array.prototype.map()
: Can be used to create a new array with the converted elements, which can then be joined together using join()
.lodash.stringify()
or xml-js
.Overall, this benchmark provides a useful comparison between two common approaches to converting arrays to strings in JavaScript, highlighting the trade-offs between simplicity, readability, and performance.