--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
genHexString1 | |
genHexString2 | |
genHexString3 | |
genHexString4 | |
genHexString5 |
Test name | Executions per second |
---|---|
genHexString1 | 503242.8 Ops/sec |
genHexString2 | 354289.7 Ops/sec |
genHexString3 | 458107.3 Ops/sec |
genHexString4 | 290037.8 Ops/sec |
genHexString5 | 460392.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net!
Benchmark Definition Json
The provided JSON represents a benchmark definition, which consists of:
genHexString1(len)
: Uses crypto.getRandomValues
to generate an array of bytes, converts them to hex, and pads each byte with leading zeros.genHexString2(len)
: Uses a predefined string hex
to generate a random hex string by randomly selecting characters from this string.genHexString3(len)
: Similar to genHexString1
, but uses the window.crypto.getRandomValues
API directly.genHexString4(len)
: Generates a random hex string by randomly selecting characters from a predefined string hexChars
.genHexString5(len)
: Uses the btoa
function to convert an array of bytes to a hex string, which is then truncated to the desired length.Individual Test Cases
Each test case represents a single benchmark, where a specific function from the script preparation code is called with a fixed input length (20). The test cases are:
genHexString1
genHexString2
genHexString3
genHexString4
genHexString5
These test cases will measure the performance of each function under different conditions.
Comparison and Options
The comparison here is between five different approaches to generate random hexadecimal strings:
genHexString1
: Uses crypto.getRandomValues
to generate an array of bytes, converts them to hex, and pads each byte with leading zeros.crypto
module).genHexString2
: Uses a predefined string hex
to generate a random hex string by randomly selecting characters from this string.genHexString3
: Similar to genHexString1
, but uses the window.crypto.getRandomValues
API directly.genHexString1
.genHexString1
.genHexString4
: Generates a random hex string by randomly selecting characters from a predefined string hexChars
.genHexString2
, but with a larger character set.genHexString5
: Uses the btoa
function to convert an array of bytes to a hex string, which is then truncated to the desired length.btoa
.Library Usage
None of the functions in the script preparation code explicitly use any external libraries. However, some browsers (like Chrome) provide APIs like crypto
or btoa
that are implemented as part of the browser's JavaScript engine.
Special JS Feature or Syntax
There is no explicit usage of special JavaScript features or syntax in this benchmark definition. The code uses standard JavaScript syntax and built-in functions.
Other Alternatives
If you were to replace these benchmarks with alternative approaches, some options might include:
window.crypto.getRandomValues
API.random.org
or jsrand
.