var bytes = [84,104,105,115,32,105,115,32,97,32,115,97,109,112,108,101,32,112,97,114,97,103,114,97,112,104,46];
var bufferArray = new Uint16Array(bytes);
var decoder = new TextDecoder('utf8'); // default 'utf-8' or 'utf8'
String.fromCharCode.apply(null, bufferArray);
decoder.decode(bufferArray);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
String.fromCharCode | |
TextDecoder |
Test name | Executions per second |
---|---|
String.fromCharCode | 1370038.8 Ops/sec |
TextDecoder | 2718375.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Description
The benchmark measures the performance difference between two approaches to convert a Uint16Array (a buffer containing 16-bit unsigned integers) to a string:
apply
method to call the charCodeAt
function for each element in the array, concatenating the results into a single string.TextDecoder
API to decode the buffer as UTF-8 (or 'utf-8' with a lowercase 'u').Options Comparison
The two approaches have different trade-offs:
Library
In this benchmark, a Uint16Array
is used as a buffer. A Uint16Array is a typed array that represents an array of 16-bit unsigned integers. It's a convenient data structure for working with binary data in JavaScript.
Special JS Feature/Syntax
There are no special JavaScript features or syntaxes used in this benchmark. The focus is on the performance comparison between two simple, well-known approaches to converting a buffer to a string.
Other Alternatives
If you're interested in exploring other alternatives, here are some options:
In summary, this benchmark provides a simple and insightful comparison of two approaches for converting a buffer to a string in JavaScript. The results can help developers make informed decisions about which method to use depending on their specific requirements and constraints.