var [testDecode, testFCCodeBuf, testFCCodeArr, testFCPointBuf, testFCPointArr] = (function(){
"use strict";
var bytesArray = [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 Uint8Array(bytesArray);
var decoder = new TextDecoder(); // default 'utf-8' or 'utf8'
var {fromCharCode, fromCodePoint} = String;
var retStr = "";
self.getRetStr = function() {return retStr};
function testDecode() {
for (var i=0; i < 256; i=i+1|0) retStr = "" + decoder.decode(bufferArray);
}
function testFCCodeBuf() {
for (var i=0; i < 256; i=i+1|0) retStr = "" + fromCharCode.apply(null, bufferArray);
}
function testFCCodeArr() {
for (var i=0; i < 256; i=i+1|0) retStr = "" + fromCharCode.apply(null, bytesArray);
}
function testFCPointBuf() {
for (var i=0; i < 256; i=i+1|0) retStr = "" + fromCodePoint.apply(null, bufferArray);
}
function testFCPointArr() {
for (var i=0; i < 256; i=i+1|0) retStr = "" + fromCodePoint.apply(null, bytesArray);
}
return [testDecode, testFCCodeBuf, testFCCodeArr, testFCPointBuf, testFCPointArr];
})();
testDecode();
testFCCodeBuf();
testFCCodeArr();
testFCPointBuf();
testFCPointArr();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TextDecoder on Uint8Array | |
String.fromCharCode on Uint8Array | |
String.fromCharCode on Array | |
String.fromCodePoint on Uint8Array | |
String.fromCodePoint on Array |
Test name | Executions per second |
---|---|
TextDecoder on Uint8Array | 29480.1 Ops/sec |
String.fromCharCode on Uint8Array | 15879.3 Ops/sec |
String.fromCharCode on Array | 57867.5 Ops/sec |
String.fromCodePoint on Uint8Array | 12409.2 Ops/sec |
String.fromCodePoint on Array | 27346.9 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of three different ways to convert between code points and Unicode strings:
TextDecoder
on Uint8Array
String.fromCharCode
on Uint8Array
String.fromCharCode
on an arrayThese conversions are essential in JavaScript, as they can be used to decode binary data, such as images or audio files.
Options Compared
The benchmark compares the performance of each option across two scenarios:
Uint8Array
to a string.Uint8Array
to a string (for Unicode characters).Pros and Cons
Here's a brief summary of the pros and cons of each option:
String.fromCharCode
for small inputs.String.fromCharCode
on Uint8Array
, handles Unicode characters correctly.TextDecoder
, can be slower for very large inputs.Library and Purpose
In this benchmark, the following libraries are used:
TextDecoder
: A built-in JavaScript library for decoding binary data into strings.String.fromCharCode
(and its array equivalent): Built-in JavaScript functions for converting code points to Unicode characters.Special JS Feature or Syntax
None of the tested options rely on special JavaScript features or syntax beyond what's covered in the ECMAScript standard.
Other Alternatives
If you need to optimize these conversions, consider using:
In conclusion, this benchmark helps you understand the performance differences between TextDecoder
, String.fromCharCode
, and its array equivalent when converting code points to Unicode strings from a fixed-size array or Uint8Array
. The choice of option depends on your specific use case and performance requirements.