unique-username-generator icon indicating copy to clipboard operation
unique-username-generator copied to clipboard

Edge runtime support

Open v1s10n-4 opened this issue 1 year ago • 1 comments

Add compatibility with Edge Runtime and Improved Performance

Hello, first of all that package is very useful to me, thanks for bringing it to life. In this pull request, I've made the following improvements:

  1. Edge Runtime Support: The current implementation of the getRandomUint32() function relies on the crypto module, which is not available in all environments, such as the Edge runtime (which is built on top of v8 so no crypto.randomBytes there). I've updated the function to use a more portable approach that doesn't require the crypto module, ensuring that the library can be used in a wider range of environments.

  2. Improved Performance: The updated getRandomUint32 function leverages bitwise operations that should provide a noticeable performance boost, especially in scenarios where the function is called frequently. I Included a benchmark script to compare the performance of the new and old implementations, demonstrating the improvements (on the benchmark branch of my fork). Here's the results on my machine:

$ npm run benchmark
Newer version x 17,870,662 ops/sec ±0.61% (88 runs sampled)
Older version x 183,173 ops/sec ±3.27% (81 runs sampled)

Here are the key changes I've made:

  • Removed the dependency on the crypto module and implemented a pure JavaScript solution for generating random 32-bit integers.
  • Optimized the getRandomUint32() function by using bitwise operations for clamping the min and max values, and pre-calculating the range to avoid repeated computations.

Please let me know if you have any feedback or questions.

Thank you for your time and consideration!

v1s10n-4 avatar Apr 07 '24 13:04 v1s10n-4

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

sonarqubecloud[bot] avatar Apr 07 '24 13:04 sonarqubecloud[bot]