ouid-h47
v1.0.2
Published
A Node.js package to generate optimized unique IDs with timestamp and random component.
Downloads
6
Maintainers
Readme
OUID (Optimized Unique Identifier)
Node.js package designed to generate optimized unique identifiers (OUID) that incorporate both a timestamp and a random component. These identifiers are suitable for a variety of applications, including database keys, session identifiers, and unique references in distributed systems.
Features
- Optimized Unique Identifiers (OUID): Combines a timestamp with a random string to ensure uniqueness and optimize sorting.
- Timestamp-Based: IDs include a timestamp component, which makes them naturally sortable by creation time.
- High Performance: Designed to generate unique IDs quickly and efficiently.
- Simple API: Easy-to-use function to generate IDs with no configuration required.
- Collision Resistant: Built to minimize the risk of generating duplicate IDs even in high-throughput environments.
Benefits
- Improved Performance: By including a timestamp, OUIDs can be indexed and sorted more efficiently in databases.
- Distributed System Friendly: The randomness and timestamp components ensure uniqueness across distributed systems.
- Ease of Use: Simplifies the process of generating unique identifiers without needing to configure or manage external libraries.
Installation
Install the package using npm:
npm install ouid-h47
Usage
const { ouid } = require('ouid-h47');
// Generate a new optimal ID
const newID = ouid();
console.log('Generated optimal ID:', newID);
Performance
The performance chart below illustrates the time taken to insert batches of IDs into a MySQL database using ouid. Each batch represents a set of IDs generated and inserted sequentially.
Performance Testing Details
The performance testing was conducted using a Node.js script (test-load.js
) that simulates inserting batches of IDs into a MySQL database. Each batch contains a number of IDs generated by ouid, and the time taken to insert each batch was recorded.
The data reflects the average time (in milliseconds) required to complete each batch insertion, providing insights into the efficiency and scalability of ouid in real-world database operations.
Interpretation
- Batch Insertion Time: Indicates how quickly ouid can generate and insert IDs into a database.
- Scalability: The consistent performance across batches demonstrates the package's reliability even under load.
- Optimization: By leveraging both timestamp and random components, ouid ensures efficient indexing and sorting of IDs within the database.
This performance data highlights the suitability of ouid for applications requiring fast and reliable unique identifier generation, such as high-throughput systems and distributed databases.
API
ouid()
Generates a new optimal ID composed of a timestamp and a random string.
- Returns: string - A unique ID with 26 characters (10 timestamp + 16 random).
Testing
The testing framework used for ouid is Jest. To run tests:
- Install Jest globally or locally in your project:
npm install --save-dev jest
- Create a test.js file with the following content:
const { ouid } = require('ouid-h47');
// Example test
test('Generate Optimal ID Test', () => {
const id = ouid();
expect(typeof id).toBe('string');
expect(id.length).toBe(26);
expect(id).toMatch(/^[0-9a-zA-Z]{26}$/);
});
- Add this line to package.json
"scripts": {
"test": "jest"
},
- Run Jest to execute the tests:
npm test
Contributing
Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
This project is licensed under the ISC License - see the LICENSE file for details.