-
Notifications
You must be signed in to change notification settings - Fork 4
Performance optimizations for UUID v7 generator #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aruan-creator
wants to merge
3
commits into
HowProgrammingWorks:main
from
Aruan-creator:performance-improvements
Open
Performance optimizations for UUID v7 generator #5
Aruan-creator
wants to merge
3
commits into
HowProgrammingWorks:main
from
Aruan-creator:performance-improvements
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Implement performance-optimized Node.js version (optimized.js) - Implement performance-optimized browser ESM version (optimized.mjs) - Add pre-allocated buffers to reduce memory allocation overhead - Implement random bytes pooling (1KB) to minimize crypto calls - Optimize Base64URL encoding algorithm - Add batch generation support for bulk operations - Achieve 321% performance improvement over original implementation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add comprehensive test suite with node:test framework - Test UUID format validation and Base64URL encoding - Verify monotonicity within same millisecond - Test batch generation functionality - Add performance comparison tests - Create detailed benchmark suite comparing original vs optimized - Benchmark shows 321% performance improvement - All tests passing with proper code style compliance 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit refactors the optimized timestamp generator (`optimized.js`) to significantly improve both performance and code readability. Key changes: - Replaced the custom Base64URL encoder with the native and faster `buffer.toString('base64url')`. - Removed the inefficient busy-wait loop (`waitNextMillisecond`) for handling sequence overflows. The generator now increments the timestamp, which is a more robust and CPU-friendly approach. - Optimized sequence initialization by using random bytes from the pre-filled pool instead of making a separate call to `crypto.randomInt`. - Simplified the timestamp writing logic by using `writeBigUInt64BE` for better performance and clarity. These changes result in an ~8x performance improvement based on benchmark tests, while making the code easier to understand and maintain.
🚀 PR Updated with Major Performance Improvements!
Just integrated the refactor commit from refactor/improve-timestamp-generator
branch, resulting in significant performance gains:
New Benchmark Results:
- 5.14M ops/sec (up from 3.67M)
- 195ns latency (down from 272ns)
- 521% improvement over original (up from 322%)
Key Changes from Refactor:
- Native
buffer.toString('base64url')
instead of custom encoding - Eliminated CPU-intensive busy-wait loops
- Optimized random number generation from byte pool
- More efficient BigInt operations with
writeBigUInt64BE
Comparison with PR #4:
Our implementation now outperforms PR #4 for individual operations:
- PR Performance optimizations for UUID v7 generator #5 (ours): 5.14M ops/sec, 195ns latency
- PR Implement high-performance 48-bit timestamp generator #4 : 4.46M ops/sec, 224ns latency
- 15% faster while maintaining full UUIDv7 standard compliance!
All tests still passing ✅
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces significant performance optimizations for the UUID v7 generator implementation while maintaining full compliance with the UUIDv7 specification (RFC 9562).
Performance Improvements
Key Optimizations
Memory Management
Algorithm Improvements
Testing
All tests pass successfully:
Benchmark Results
Files Changed
Timestamp48/optimized.js
- Optimized Node.js implementationTimestamp48/optimized.mjs
- Optimized browser ESM implementationTimestamp48/test.js
- Comprehensive test suiteTimestamp48/benchmark.js
- Performance benchmarking toolsCompatibility
This implementation focuses on real-world performance while maintaining standards compliance, making it suitable for high-throughput production environments.