-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade to v2.0.0 with enhanced nanoid functions and optimizations
- Loading branch information
Showing
6 changed files
with
327 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- PLACE IMPLEMENTATION A 'a_nanoid()' HERE |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- PLACE IMPLEMENTATION B 'b_nanoid()' HERE |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
DO | ||
$$ | ||
DECLARE | ||
startTime timestamp; | ||
endTime timestamp; | ||
durationA interval; | ||
durationB interval; | ||
numLoops int := 100000; | ||
counter int; | ||
dummyResult text; | ||
BEGIN | ||
|
||
-- Benchmarking A nanoid() | ||
RAISE NOTICE '-----------------------------'; | ||
RAISE NOTICE 'Starting benchmark for A nanoid() for % loops...', numLoops; | ||
startTime := clock_timestamp(); | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
dummyResult := a_nanoid(); | ||
dummyResult := a_nanoid(5, '23456789abcdefghijklmnopqrstuvwxyz'); | ||
dummyResult := a_nanoid(11, '_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,'); | ||
dummyResult := a_nanoid(48, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'); | ||
END LOOP; | ||
endTime := clock_timestamp(); | ||
durationA := endTime - startTime; | ||
RAISE NOTICE 'A nanoid() took %', durationA; | ||
RAISE NOTICE '-----------------------------'; | ||
|
||
-- Benchmarking B nanoid() | ||
RAISE NOTICE 'Starting benchmark for B nanoid() for % loops...', numLoops; | ||
startTime := clock_timestamp(); | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
dummyResult := b_nanoid(); | ||
dummyResult := b_nanoid(5, '23456789abcdefghijklmnopqrstuvwxyz'); | ||
dummyResult := b_nanoid(11, '_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,'); | ||
dummyResult := b_nanoid(48, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'); | ||
END LOOP; | ||
endTime := clock_timestamp(); | ||
durationB := endTime - startTime; | ||
RAISE NOTICE 'B nanoid() took %', durationB; | ||
RAISE NOTICE '-----------------------------'; | ||
|
||
-- Compare | ||
IF durationA < durationB THEN | ||
RAISE NOTICE 'A nanoid() is faster by %', durationB - durationA; | ||
ELSIF durationA > durationB THEN | ||
RAISE NOTICE 'B nanoid() is faster by %', durationA - durationB; | ||
ELSE | ||
RAISE NOTICE 'Both functions have comparable performance.'; | ||
END IF; | ||
|
||
RAISE NOTICE '-----------------------------'; | ||
|
||
END | ||
$$; | ||
|
||
-- TODO: | ||
-- EXTRACT(EPOCH FROM (timestamp1 - timestamp2)) | ||
-- ROUND() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
DO | ||
$$ | ||
DECLARE | ||
generated_id text; | ||
counter int; | ||
numLoops int := 1000; | ||
BEGIN | ||
-- Default parameters | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
generated_id := nanoid(); | ||
RAISE NOTICE '%', generated_id; | ||
ASSERT LENGTH(generated_id) = 21, 'Default nanoid length is incorrect'; | ||
ASSERT generated_id ~ '^[-_a-zA-Z0-9]*$', 'Default nanoid contains invalid characters'; | ||
END LOOP; | ||
|
||
-- Size 12, default alphabet | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
generated_id := nanoid(12); | ||
RAISE NOTICE '%', generated_id; | ||
ASSERT LENGTH(generated_id) = 12, 'Size 12 nanoid length is incorrect'; | ||
ASSERT generated_id ~ '^[-_a-zA-Z0-9]*$', 'Size 12 nanoid contains invalid characters'; | ||
END LOOP; | ||
|
||
-- Size 25, default alphabet | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
generated_id := nanoid(25); | ||
RAISE NOTICE '%', generated_id; | ||
ASSERT LENGTH(generated_id) = 25, 'Size 25 nanoid length is incorrect'; | ||
ASSERT generated_id ~ '^[-_a-zA-Z0-9]*$', 'Size 25 nanoid contains invalid characters'; | ||
END LOOP; | ||
|
||
-- Default size (21), custom alphabet (only lowercase) | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
generated_id := nanoid(21, 'abcdefghijklmnopqrstuvwxyz'); | ||
RAISE NOTICE '%', generated_id; | ||
ASSERT LENGTH(generated_id) = 21, 'Size 21 (only lowercase) nanoid length is incorrect'; | ||
ASSERT generated_id ~ '^[a-z]*$', 'Size 21 (only lowercase) nanoid contains invalid characters'; | ||
END LOOP; | ||
|
||
-- Size 15, custom alphabet (only numbers) | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
generated_id := nanoid(15, '0123456789'); | ||
RAISE NOTICE '%', generated_id; | ||
ASSERT LENGTH(generated_id) = 15, 'Size 15 (only numbers) nanoid length is incorrect'; | ||
ASSERT generated_id ~ '^[0-9]*$', 'Size 15 (only numbers) nanoid contains invalid characters'; | ||
END LOOP; | ||
|
||
-- Size 17, custom alphabet (uppercase + numbers) | ||
FOR counter IN 1..numLoops | ||
LOOP | ||
generated_id := nanoid(17, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'); | ||
RAISE NOTICE '%', generated_id; | ||
ASSERT LENGTH(generated_id) = 17, 'Size 17 (uppercase + numbers) nanoid length is incorrect'; | ||
ASSERT generated_id ~ '^[A-Z0-9]*$', 'Size 17 (uppercase + numbers) nanoid contains invalid characters'; | ||
END LOOP; | ||
|
||
-- -- Intentional false positive: use default size but with a mismatched regex pattern | ||
-- FOR counter IN 1..numLoops | ||
-- LOOP | ||
-- generated_id := nanoid(); | ||
-- RAISE NOTICE '%', generated_id; | ||
-- -- This will fail because we're purposefully using a wrong pattern | ||
-- ASSERT generated_id ~ '^[XYZ]*$', 'Intentional false positive detected'; | ||
-- END LOOP; | ||
|
||
RAISE NOTICE 'All tests passed successfully!'; | ||
END | ||
$$; |
Oops, something went wrong.