Browse Source

docs: add simple enhancedEntropy benchmarks

Sv443 5 months ago
parent
commit
305cb6fe4a
2 changed files with 37 additions and 3 deletions
  1. 36 2
      README.md
  2. 1 1
      lib/crypto.ts

+ 36 - 2
README.md

@@ -966,7 +966,7 @@ Returns a random number between `min` and `max` (inclusive).
 If only one argument is passed, it will be used as the `max` value and `min` will be set to 0.  
   
 If `enhancedEntropy` is set to true (false by default), the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) is used for generating the random numbers.  
-Note that this takes MUCH longer, but the generated IDs will have a higher entropy.  
+Note that this makes the function call take longer, but the generated IDs will have a higher entropy.  
   
 <details><summary><b>Example - click to view</b></summary>
 
@@ -977,6 +977,22 @@ randRange(0, 10);       // 4
 randRange(10, 20);      // 17
 randRange(10);          // 7
 randRange(0, 10, true); // 4 (the devil is in the details)
+
+
+function benchmark(enhancedEntropy: boolean) {
+  const timestamp = Date.now();
+  for(let i = 0; i < 100_000; i++)
+    randRange(0, 100, enhancedEntropy);
+  console.log(`Generated 100k in ${Date.now() - timestamp}ms`)
+}
+
+// using Math.random():
+benchmark(false); // Generated 100k in 90ms
+
+// using crypto.getRandomValues():
+benchmark(true);  // Generated 100k in 461ms
+
+// about a 5x slowdown, but the generated numbers are more entropic
 ```
 </details>
 
@@ -1926,7 +1942,7 @@ You may change the radix to get digits from different numerical systems.
 Use 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal and 36 for alphanumeric.  
   
 If `enhancedEntropy` is set to true (false by default), the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) is used for generating the random numbers.  
-Note that this takes MUCH longer, but the generated IDs will have a higher entropy.  
+Note that this makes the function call take longer, but the generated IDs will have a higher entropy.  
   
 If `randomCase` is set to true (which it is by default), the generated ID will contain both upper and lower case letters.  
 This randomization is also affected by the `enhancedEntropy` setting, unless there are no alphabetic characters in the output in which case it will be skipped.  
@@ -1943,6 +1959,24 @@ randomId(10);                  // "f86cd354a4"       (length 10, radix 16)
 randomId(10, 2);               // "1010001101"       (length 10, radix 2)
 randomId(10, 10);              // "0183428506"       (length 10, radix 10)
 randomId(10, 36, false, true); // "z46jFPa37R"       (length 10, radix 36, random case)
+
+
+function benchmark(enhancedEntropy: boolean, randomCase: boolean) {
+  const timestamp = Date.now();
+  for(let i = 0; i < 10_000; i++)
+    randomId(16, 36, enhancedEntropy, randomCase);
+  console.log(`Generated 10k in ${Date.now() - timestamp}ms`)
+}
+
+// using Math.random():
+benchmark(false, false); // Generated 10k in 239ms
+benchmark(false, true);  // Generated 10k in 248ms
+
+// using crypto.getRandomValues():
+benchmark(true, false);  // Generated 10k in 1076ms
+benchmark(true, true);   // Generated 10k in 1054ms
+
+// 3rd and 4th have a similar time, but in reality the 4th blocks the event loop for much longer
 ```
 </details>
 

+ 1 - 1
lib/crypto.ts

@@ -72,7 +72,7 @@ export async function computeHash(input: string | ArrayBuffer, algorithm = "SHA-
  * ⚠️ Not suitable for generating anything related to cryptography! Use [SubtleCrypto's `generateKey()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey) for that instead.
  * @param length The length of the ID to generate (defaults to 16)
  * @param radix The [radix](https://en.wikipedia.org/wiki/Radix) of each digit (defaults to 16 which is hexadecimal. Use 2 for binary, 10 for decimal, 36 for alphanumeric, etc.)
- * @param enhancedEntropy If set to true, uses [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for better cryptographic randomness (this also makes it take MUCH longer to generate)  
+ * @param enhancedEntropy If set to true, uses [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for better cryptographic randomness (this also makes it take longer to generate)  
  * @param randomCase If set to false, the generated ID will be lowercase only - also makes use of the `enhancedEntropy` parameter unless the output doesn't contain letters
  */
 export function randomId(length = 16, radix = 16, enhancedEntropy = false, randomCase = true): string {