unique-username-generator
unique-username-generator copied to clipboard
Edge runtime support
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:
-
Edge Runtime Support: The current implementation of the
getRandomUint32()function relies on thecryptomodule, which is not available in all environments, such as the Edge runtime (which is built on top of v8 so nocrypto.randomBytesthere). I've updated the function to use a more portable approach that doesn't require thecryptomodule, ensuring that the library can be used in a wider range of environments. -
Improved Performance: The updated
getRandomUint32function 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 thebenchmarkbranch 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
cryptomodule and implemented a pure JavaScript solution for generating random 32-bit integers. - Optimized the
getRandomUint32()function by using bitwise operations for clamping theminandmaxvalues, 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!
Quality Gate passed
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
No data about Coverage
No data about Duplication