Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Random hex string generation benchmark
(version: 0)
Comparing performance of:
genHexString1 vs genHexString2 vs genHexString3 vs genHexString4 vs genHexString5
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function genHexString1(len) { return Array.from(crypto.getRandomValues(new Uint8Array(len / 2 ))).map(b => b.toString(16).padStart(2, '0')).join(''); } function genHexString2(len) { const hex = '0123456789abcdef'; let output = ''; for (let i = 0; i < len; ++i) { output += hex.charAt(Math.floor(Math.random() * hex.length)); } return output; } function genHexString3(len) { let arr = new Uint8Array((len / 2)); window.crypto.getRandomValues(arr); return Array.from(arr, dec => ('0' + dec.toString(16)).substr(-2)).join(''); } function genHexString4(len) { const hexChars = '0123456789abcdef'; return [...Array(len)].map(() => hexChars[Math.floor(Math.random() * 16)]).join(''); } function genHexString5(len) { let arr = new Uint8Array(len / 2); crypto.getRandomValues(arr); return btoa(String.fromCharCode.apply(null, arr)).replace(/[^a-f0-9]/g, '').substr(0, len); }
Tests:
genHexString1
const hex = genHexString1(20)
genHexString2
const hex = genHexString2(20)
genHexString3
const hex = genHexString3(20)
genHexString4
const hex = genHexString4(20)
genHexString5
const hex = genHexString5(20)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
genHexString1
genHexString2
genHexString3
genHexString4
genHexString5
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
matthex
parse hex-string to 'bytes like object' (Uint8Array) in javascript
array buffer to hex conversion
array buffer to hex conversion 2
Comments
Confirm delete:
Do you really want to delete benchmark?