Script Preparation code:
x
 
var saltVal = crypto.getRandomValues(new Uint8Array(16));
function arrayBufferToHexString(bytes) {
    bytes = new Uint8Array(bytes);
    var hex = new Array(bytes.length);
    for (let i = 0; i < bytes.length; i++) {
        hex[i] = ("0" + bytes[i].toString(16)).slice(-2);
    }
    return hex.join("");
}
function bytesToHexString(bytes) {
    bytes = new Uint8Array(bytes);
    var hexBytes = [];
    for (var i = 0; i < bytes.length; i++) {
        var byteString = bytes[i].toString(16);
        if (byteString.length < 2) byteString = "0" + byteString;
        hexBytes.push(byteString);
    }
    return hexBytes.join("");
}
function buf2hex(buffer) { // buffer is an ArrayBuffer
  return [...new Uint8Array(buffer)]
      .map(x => x.toString(16).padStart(2, '0'))
      .join('');
}
function hex(arrayBuffer)
{
    const asciiCodes = new Uint8Array(
        Array.prototype.map.call(
            "0123456789abcdef",
            char => char.charCodeAt()
        )
    );
  
    const buff = new Uint8Array(arrayBuffer);
    const charCodes = new Uint8Array(buff.length * 2);
    for (let i = 0; i < buff.length; ++i)
    {
        charCodes[i * 2] = asciiCodes[buff[i] >>> 4];
        charCodes[i * 2 + 1] = asciiCodes[buff[i] & 0xf];
    }
    return String.fromCharCode(...charCodes);
}
function hex2(arrayBuffer)
{
    return Array.prototype.map.call(
        new Uint8Array(arrayBuffer),
        n => n.toString(16).padStart(2, "0")
    ).join("");
}
function arrayBufferToBase64(buffer) {
  const byteArray = new Uint8Array(buffer);
  const binaryString = byteArray.reduce((acc, byte) => acc + String.fromCharCode(byte), "");
  return btoa(binaryString);
}
function arrayBufferToBase64loop( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return window.btoa( binary );
}
function bytesToHexString2(bytes) {
    bytes = new Uint8Array(bytes);
    var hexBytes = '';
    for (var i = 0; i < bytes.length; i++) {
        var byteString = bytes[i].toString(16);
        if (byteString.length < 2) byteString = "0" + byteString;
        hexBytes += byteString;
    }
    return hexBytes;
}
function bytesToHexString3(bytes) {
    bytes = new Uint8Array(bytes);
    var hexBytes = '';
    for (var i = 0; i < bytes.length; i++) {
        var byteString = bytes[i].toString(16);
        if (byteString.length < 2) hexBytes += "0";
        hexBytes += byteString;
    }
    return hexBytes;
}
function bytesToHexString4(bytes) {
    bytes = new Uint8Array(bytes);
    var hexBytes = '';
    for (var i = 0; i < bytes.length; i++) {
        hexBytes += bytes[i].toString(16).padStart(2, '0');
    }
    return hexBytes;
}
Tests:
  • directly set

     
    arrayBufferToHexString(saltVal);
  • push values

     
    bytesToHexString(saltVal)
  • buf2hex()

     
    buf2hex(saltVal)
  • precomputed ASCII with shift

     
    hex(saltVal)
  • array prototype map

     
    hex2(saltVal)
  • base64

     
    arrayBufferToBase64(saltVal)
  • base64 loop

     
    arrayBufferToBase64loop(saltVal)
  • string +

     
    bytesToHexString2(saltVal)
  • string + 2

     
    bytesToHexString3(saltVal)
  • string + pad

     
    bytesToHexString4(saltVal)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    directly set
    push values
    buf2hex()
    precomputed ASCII with shift
    array prototype map
    base64
    base64 loop
    string +
    string + 2
    string + pad

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 months ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Chrome 131 on Windows
View result in a separate tab
Test name Executions per second
directly set 2449344.0 Ops/sec
push values 3418329.5 Ops/sec
buf2hex() 1701248.1 Ops/sec
precomputed ASCII with shift 864891.2 Ops/sec
array prototype map 1193146.8 Ops/sec
base64 3353756.8 Ops/sec
base64 loop 4347946.5 Ops/sec
string + 5316500.0 Ops/sec
string + 2 5316376.5 Ops/sec
string + pad 4910442.0 Ops/sec