-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.js
More file actions
621 lines (575 loc) · 23.5 KB
/
benchmark.js
File metadata and controls
621 lines (575 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
'use strict';
const { performance } = require('perf_hooks');
const crypto = require('crypto');
const { generateGSID } = require('./3-Tech-specs/gsid.js');
const GSIDv1 = require('./1-Prompt/gsid.js');
const GSIDv2 = require('./2-Chat-steps/gsid.js');
const { generateId: generateSimpleId } = require('./0-Simple/id.js');
const { generateId: generateGSIDManual } = require('./5-Manual/gsid.js');
let generateID;
const calcEntropy = (charFreq, totalChars) => {
let entropy = 0;
for (const [, count] of Object.entries(charFreq)) {
const prob = count / totalChars;
entropy -= prob * Math.log2(prob);
}
return entropy;
};
const benchmark = (gen, name, size = 1000000) => {
console.log(`\n=== ${name} Benchmark & Analysis ===`);
const memStart = process.memoryUsage();
const timeStart = performance.now();
const ids = [];
for (let i = 0; i < size; i++) {
ids.push(gen());
}
const timeEnd = performance.now();
const memEnd = process.memoryUsage();
const duration = timeEnd - timeStart;
const rate = size / (duration / 1000);
const avgTime = (duration / size) * 1000;
const memDelta = {
rss: memEnd.rss - memStart.rss,
heapUsed: memEnd.heapUsed - memStart.heapUsed,
heapTotal: memEnd.heapTotal - memStart.heapTotal,
external: memEnd.external - memStart.external,
arrayBuffers: memEnd.arrayBuffers - memStart.arrayBuffers,
};
console.log(`⏱️ Performance:`);
console.log(` Duration: ${duration.toFixed(2)}ms`);
console.log(` Rate: ${rate.toLocaleString()} IDs/second`);
console.log(` Average time per ID: ${avgTime.toFixed(3)}μs`);
console.log(` Memory Delta:`, memDelta);
const unique = new Set();
let collisions = 0;
const charFreq = {};
let totalChars = 0;
let unsafeChars = 0;
const lengths = [];
const entropySamples = [];
for (let i = 0; i < size; i++) {
const id = ids[i];
if (unique.has(id)) {
collisions++;
} else {
unique.add(id);
}
lengths.push(id.length);
totalChars += id.length;
for (const char of id) {
charFreq[char] = (charFreq[char] || 0) + 1;
}
const unsafePattern = /[^A-Za-z0-9\-_]/g;
const unsafeMatches = id.match(unsafePattern);
if (unsafeMatches) {
unsafeChars += unsafeMatches.length;
}
if (i < 10000) {
entropySamples.push(id);
}
}
const collisionRate = (collisions / size) * 100;
console.log(`\n🔍 Collision Analysis:`);
console.log(` Total samples: ${size.toLocaleString()}`);
console.log(` Unique samples: ${unique.size.toLocaleString()}`);
console.log(` Collisions: ${collisions}`);
console.log(` Collision rate: ${collisionRate.toFixed(8)}%`);
const avgLength = lengths.reduce((sum, len) => sum + len, 0) / lengths.length;
const uniqueLengths = [...new Set(lengths)];
let minLen = lengths[0];
let maxLen = lengths[0];
for (let i = 1; i < lengths.length; i++) {
if (lengths[i] < minLen) minLen = lengths[i];
if (lengths[i] > maxLen) maxLen = lengths[i];
}
console.log(`\n📏 Size Analysis:`);
console.log(` Average size: ${avgLength.toFixed(1)} characters`);
const consistency = uniqueLengths.length === 1 ? 'Consistent' : 'Variable';
console.log(` Size consistency: ${consistency}`);
console.log(` Size range: ${minLen} - ${maxLen} characters`);
const unsafePercent = (unsafeChars / totalChars) * 100;
const isUrlSafe = unsafePercent === 0;
console.log(`\n🌐 URL Safety:`);
console.log(` URL-safe characters: ${isUrlSafe ? 'Yes' : 'No'}`);
const unsafePercentStr = `${unsafePercent.toFixed(2)}%`;
console.log(` Unsafe character percentage: ${unsafePercentStr}`);
console.log(` Total unsafe characters: ${unsafeChars}`);
// Keep raw counts for entropy calculation, create percentage version for
// display
const charFreqPercent = {};
for (const char in charFreq) {
charFreqPercent[char] = (charFreq[char] / totalChars) * 100;
}
const sortedChars = Object.entries(charFreqPercent).sort(
([, a], [, b]) => b - a,
);
console.log(`\n📊 Character Distribution (top 10):`);
sortedChars.slice(0, 10).forEach(([char, percent]) => {
console.log(` '${char}': ${percent.toFixed(2)}%`);
});
const totalEntropy = calcEntropy(charFreq, totalChars);
const entropyPerChar = totalEntropy;
const entropyPerID = totalEntropy * avgLength;
console.log(`\n🎲 Entropy Analysis (based on ${size.toLocaleString()} IDs):`);
console.log(` Total Characters: ${totalChars.toLocaleString()}`);
console.log(` Unique Characters: ${Object.keys(charFreq).length}`);
console.log(` Entropy per Character: ${entropyPerChar.toFixed(4)} bits`);
console.log(
` Entropy per ID (${avgLength.toFixed(1)} chars): ` +
`${entropyPerID.toFixed(4)} bits`,
);
const theoreticalMaxPerChar = Math.log2(64);
const theoreticalMaxPerID = theoreticalMaxPerChar * avgLength;
console.log(
` Theoretical Max per Character: ` +
`${theoreticalMaxPerChar.toFixed(4)} bits`,
);
console.log(
` Theoretical Max per ID: ${theoreticalMaxPerID.toFixed(4)} bits`,
);
const efficiency = (entropyPerID / theoreticalMaxPerID) * 100;
console.log(` Efficiency: ${efficiency.toFixed(2)}%`);
return {
samples: ids,
duration,
rate,
memoryDelta: memDelta,
collisionRate,
avgSize: avgLength,
isUrlSafe,
avgEntropy: entropyPerChar,
entropyPerID,
efficiency,
distribution: charFreq,
};
};
const runBenchmarks = async () => {
console.log('🚀 Starting Comprehensive ID Generator Benchmark');
console.log('='.repeat(60));
const sampleSize = 1000000;
const idjs = await import('./4-By-example/id.mjs');
generateID = idjs.generateID;
const webjs = await import('./5-Manual/gsid.mjs');
const generateGSIDWeb = webjs.generateId;
const gsidv1Instance = new GSIDv1();
const gsidv2Instance = new GSIDv2();
const gsidResults = benchmark(
() => generateGSID(),
'GSID (Tech-specs)',
sampleSize,
);
const gsidv1Results = benchmark(
() => gsidv1Instance.generate(),
'GSID v1 (Prompt)',
sampleSize,
);
const gsidv2Results = benchmark(
() => gsidv2Instance.generate(),
'GSID v2 (Chat-steps)',
sampleSize,
);
const idResults = await benchmark(
() => generateID(),
'ID (By-example)',
sampleSize,
);
const simpleResults = benchmark(
() => generateSimpleId(),
'Simple (0-Simple)',
sampleSize,
);
const manualResults = benchmark(
() => generateGSIDManual(),
'GSID (5-Manual)',
sampleSize,
);
const webResults = benchmark(
() => generateGSIDWeb(),
'GSID (5-Manual-Web)',
sampleSize,
);
const uuidResults = benchmark(
() => crypto.randomUUID(),
'UUID v4',
sampleSize,
);
console.log('\n' + '='.repeat(60));
console.log('📊 COMPREHENSIVE COMPARISON SUMMARY');
console.log('='.repeat(60));
console.log('\n⚡ Performance Comparison:');
const gsidRate = gsidResults.rate.toLocaleString();
const gsidDuration = gsidResults.duration.toFixed(2);
const gsidPerf = `${gsidRate} IDs/sec (${gsidDuration}ms)`;
console.log(` GSID (Tech-specs): ${gsidPerf}`);
const gsidv1Rate = gsidv1Results.rate.toLocaleString();
const gsidv1Duration = gsidv1Results.duration.toFixed(2);
const gsidv1Perf = `${gsidv1Rate} IDs/sec (${gsidv1Duration}ms)`;
console.log(` GSID v1 (Prompt): ${gsidv1Perf}`);
const gsidv2Rate = gsidv2Results.rate.toLocaleString();
const gsidv2Duration = gsidv2Results.duration.toFixed(2);
const gsidv2Perf = `${gsidv2Rate} IDs/sec (${gsidv2Duration}ms)`;
console.log(` GSID v2 (Chat): ${gsidv2Perf}`);
const simpleRate = simpleResults.rate.toLocaleString();
const simpleDuration = simpleResults.duration.toFixed(2);
const simplePerf = `${simpleRate} IDs/sec (${simpleDuration}ms)`;
console.log(` Simple (0-Simple): ${simplePerf}`);
const idRate = idResults.rate.toLocaleString();
const idDuration = idResults.duration.toFixed(2);
const idPerf = `${idRate} IDs/sec (${idDuration}ms)`;
console.log(` ID (By-example): ${idPerf}`);
const manualRate = manualResults.rate.toLocaleString();
const manualDuration = manualResults.duration.toFixed(2);
const manualPerf = `${manualRate} IDs/sec (${manualDuration}ms)`;
console.log(` GSID (5-Manual): ${manualPerf}`);
const webRate = webResults.rate.toLocaleString();
const webDuration = webResults.duration.toFixed(2);
const webPerf = `${webRate} IDs/sec (${webDuration}ms)`;
console.log(` GSID (5-Manual-Web): ${webPerf}`);
const uuidRate = uuidResults.rate.toLocaleString();
const uuidDuration = uuidResults.duration.toFixed(2);
const uuidPerf = `${uuidRate} IDs/sec (${uuidDuration}ms)`;
console.log(` UUID v4: ${uuidPerf}`);
const fastest = Math.max(
gsidResults.rate,
gsidv1Results.rate,
gsidv2Results.rate,
simpleResults.rate,
idResults.rate,
manualResults.rate,
webResults.rate,
uuidResults.rate,
);
let fastestName;
if (fastest === gsidResults.rate) {
fastestName = 'GSID (Tech-specs)';
} else if (fastest === gsidv1Results.rate) {
fastestName = 'GSID v1 (Prompt)';
} else if (fastest === gsidv2Results.rate) {
fastestName = 'GSID v2 (Chat)';
} else if (fastest === simpleResults.rate) {
fastestName = 'Simple (0-Simple)';
} else if (fastest === idResults.rate) {
fastestName = 'ID (By-example)';
} else if (fastest === manualResults.rate) {
fastestName = 'GSID (5-Manual)';
} else if (fastest === webResults.rate) {
fastestName = 'GSID (5-Manual-Web)';
} else {
fastestName = 'UUID v4';
}
const fastestPerf = `${fastest.toLocaleString()} IDs/sec`;
console.log(` Fastest: ${fastestName} (${fastestPerf})`);
const gsidMemoryMB = (gsidResults.memoryDelta.heapUsed / 1024 / 1024).toFixed(
2,
);
const gsidv1MemoryMB = (
gsidv1Results.memoryDelta.heapUsed /
1024 /
1024
).toFixed(2);
const gsidv2MemoryMB = (
gsidv2Results.memoryDelta.heapUsed /
1024 /
1024
).toFixed(2);
const simpleMemoryMB = (
simpleResults.memoryDelta.heapUsed /
1024 /
1024
).toFixed(2);
const idMemoryMB = (idResults.memoryDelta.heapUsed / 1024 / 1024).toFixed(2);
const manualMemoryMB = (
manualResults.memoryDelta.heapUsed /
1024 /
1024
).toFixed(2);
const webMemoryMB = (webResults.memoryDelta.heapUsed / 1024 / 1024).toFixed(
2,
);
const uuidMemoryMB = (uuidResults.memoryDelta.heapUsed / 1024 / 1024).toFixed(
2,
);
console.log('\n💾 Memory Usage Comparison:');
console.log(` GSID (Tech-specs): ${gsidMemoryMB} MB`);
console.log(` GSID v1 (Prompt): ${gsidv1MemoryMB} MB`);
console.log(` GSID v2 (Chat): ${gsidv2MemoryMB} MB`);
console.log(` Simple (0-Simple): ${simpleMemoryMB} MB`);
console.log(` ID (By-example): ${idMemoryMB} MB`);
console.log(` GSID (5-Manual): ${manualMemoryMB} MB`);
console.log(` GSID (5-Manual-Web): ${webMemoryMB} MB`);
console.log(` UUID v4: ${uuidMemoryMB} MB`);
console.log('\n🎲 Measured Entropy (per character):');
const gsidEntropyStr = `${gsidResults.avgEntropy.toFixed(4)} bits/char`;
console.log(` GSID (Tech-specs): ${gsidEntropyStr}`);
const gsidv1EntropyStr = `${gsidv1Results.avgEntropy.toFixed(4)} bits/char`;
console.log(` GSID v1 (Prompt): ${gsidv1EntropyStr}`);
const gsidv2EntropyStr = `${gsidv2Results.avgEntropy.toFixed(4)} bits/char`;
console.log(` GSID v2 (Chat): ${gsidv2EntropyStr}`);
const simpleEntropyStr = `${simpleResults.avgEntropy.toFixed(4)} bits/char`;
console.log(` Simple (0-Simple): ${simpleEntropyStr}`);
const idEntropyStr = `${idResults.avgEntropy.toFixed(4)} bits/char`;
console.log(` ID (By-example): ${idEntropyStr}`);
const manualEntropyStr = `${manualResults.avgEntropy.toFixed(4)} bits/char`;
console.log(` GSID (5-Manual): ${manualEntropyStr}`);
const webEntropyStr = `${webResults.avgEntropy.toFixed(4)} bits/char`;
console.log(` GSID (5-Manual-Web): ${webEntropyStr}`);
const uuidEntropyStr = `${uuidResults.avgEntropy.toFixed(4)} bits/char`;
console.log(` UUID v4: ${uuidEntropyStr}`);
const bestEntropy = Math.max(
gsidResults.avgEntropy,
gsidv1Results.avgEntropy,
gsidv2Results.avgEntropy,
idResults.avgEntropy,
simpleResults.avgEntropy,
manualResults.avgEntropy,
webResults.avgEntropy,
uuidResults.avgEntropy,
);
let bestEntropyName;
if (bestEntropy === gsidResults.avgEntropy) {
bestEntropyName = 'GSID (Tech-specs)';
} else if (bestEntropy === gsidv1Results.avgEntropy) {
bestEntropyName = 'GSID v1 (Prompt)';
} else if (bestEntropy === gsidv2Results.avgEntropy) {
bestEntropyName = 'GSID v2 (Chat)';
} else if (bestEntropy === idResults.avgEntropy) {
bestEntropyName = 'ID (By-example)';
} else if (bestEntropy === simpleResults.avgEntropy) {
bestEntropyName = 'Simple (0-Simple)';
} else if (bestEntropy === manualResults.avgEntropy) {
bestEntropyName = 'GSID (5-Manual)';
} else if (bestEntropy === webResults.avgEntropy) {
bestEntropyName = 'GSID (5-Manual-Web)';
} else {
bestEntropyName = 'UUID v4';
}
const bestEntropyPerf = `${bestEntropy.toFixed(4)} bits/char`;
console.log(` Best entropy: ${bestEntropyName} (${bestEntropyPerf})`);
console.log('\n🎲 Measured Entropy (per ID):');
const gsidEntropyIDStr = `${gsidResults.entropyPerID.toFixed(4)} bits/ID`;
console.log(` GSID (Tech-specs): ${gsidEntropyIDStr}`);
const gsidv1EntropyIDStr = `${gsidv1Results.entropyPerID.toFixed(4)} bits/ID`;
console.log(` GSID v1 (Prompt): ${gsidv1EntropyIDStr}`);
const gsidv2EntropyIDStr = `${gsidv2Results.entropyPerID.toFixed(4)} bits/ID`;
console.log(` GSID v2 (Chat): ${gsidv2EntropyIDStr}`);
const idEntropyIDStr = `${idResults.entropyPerID.toFixed(4)} bits/ID`;
console.log(` ID (By-example): ${idEntropyIDStr}`);
const simpleEntropyIDStr = `${simpleResults.entropyPerID.toFixed(4)} bits/ID`;
console.log(` Simple (0-Simple): ${simpleEntropyIDStr}`);
const manualEntropyIDStr = `${manualResults.entropyPerID.toFixed(4)} bits/ID`;
console.log(` GSID (5-Manual): ${manualEntropyIDStr}`);
const webEntropyIDStr = `${webResults.entropyPerID.toFixed(4)} bits/ID`;
console.log(` GSID (5-Manual-Web): ${webEntropyIDStr}`);
const uuidEntropyIDStr = `${uuidResults.entropyPerID.toFixed(4)} bits/ID`;
console.log(` UUID v4: ${uuidEntropyIDStr}`);
const bestEntropyID = Math.max(
gsidResults.entropyPerID,
gsidv1Results.entropyPerID,
gsidv2Results.entropyPerID,
idResults.entropyPerID,
simpleResults.entropyPerID,
manualResults.entropyPerID,
uuidResults.entropyPerID,
);
let bestEntropyIDName;
if (bestEntropyID === gsidResults.entropyPerID) {
bestEntropyIDName = 'GSID (Tech-specs)';
} else if (bestEntropyID === gsidv1Results.entropyPerID) {
bestEntropyIDName = 'GSID v1 (Prompt)';
} else if (bestEntropyID === gsidv2Results.entropyPerID) {
bestEntropyIDName = 'GSID v2 (Chat)';
} else if (bestEntropyID === idResults.entropyPerID) {
bestEntropyIDName = 'ID (By-example)';
} else if (bestEntropyID === simpleResults.entropyPerID) {
bestEntropyIDName = 'Simple (0-Simple)';
} else if (bestEntropyID === manualResults.entropyPerID) {
bestEntropyIDName = 'GSID (5-Manual)';
} else {
bestEntropyIDName = 'UUID v4';
}
const bestEntropyIDPerf = `${bestEntropyID.toFixed(4)} bits/ID`;
console.log(
` Best entropy per ID: ${bestEntropyIDName} (${bestEntropyIDPerf})`,
);
console.log('\n📏 Size Comparison:');
const gsidSizeStr = `${gsidResults.avgSize.toFixed(1)} characters`;
console.log(` GSID (Tech-specs): ${gsidSizeStr}`);
const gsidv1SizeStr = `${gsidv1Results.avgSize.toFixed(1)} characters`;
console.log(` GSID v1 (Prompt): ${gsidv1SizeStr}`);
const gsidv2SizeStr = `${gsidv2Results.avgSize.toFixed(1)} characters`;
console.log(` GSID v2 (Chat): ${gsidv2SizeStr}`);
const idSizeStr = `${idResults.avgSize.toFixed(1)} characters`;
console.log(` ID (By-example): ${idSizeStr}`);
const simpleSizeStr = `${simpleResults.avgSize.toFixed(1)} characters`;
console.log(` Simple (0-Simple): ${simpleSizeStr}`);
const manualSizeStr = `${manualResults.avgSize.toFixed(1)} characters`;
console.log(` GSID (5-Manual): ${manualSizeStr}`);
const webSizeStr = `${webResults.avgSize.toFixed(1)} characters`;
console.log(` GSID (5-Manual-Web): ${webSizeStr}`);
const uuidSizeStr = `${uuidResults.avgSize.toFixed(1)} characters`;
console.log(` UUID v4: ${uuidSizeStr}`);
const smallest = Math.min(
gsidResults.avgSize,
gsidv1Results.avgSize,
gsidv2Results.avgSize,
idResults.avgSize,
simpleResults.avgSize,
manualResults.avgSize,
webResults.avgSize,
uuidResults.avgSize,
);
let smallestName;
if (smallest === gsidResults.avgSize) {
smallestName = 'GSID (Tech-specs)';
} else if (smallest === gsidv1Results.avgSize) {
smallestName = 'GSID v1 (Prompt)';
} else if (smallest === gsidv2Results.avgSize) {
smallestName = 'GSID v2 (Chat)';
} else if (smallest === idResults.avgSize) {
smallestName = 'ID (By-example)';
} else if (smallest === simpleResults.avgSize) {
smallestName = 'Simple (0-Simple)';
} else if (smallest === manualResults.avgSize) {
smallestName = 'GSID (5-Manual)';
} else if (smallest === webResults.avgSize) {
smallestName = 'GSID (5-Manual-Web)';
} else {
smallestName = 'UUID v4';
}
const smallestPerf = `${smallest.toFixed(1)} chars`;
console.log(` Most compact: ${smallestName} (${smallestPerf})`);
console.log('\n🔍 Collision Rate Comparison:');
const gsidCollisionStr = `${gsidResults.collisionRate.toFixed(8)}%`;
console.log(` GSID (Tech-specs): ${gsidCollisionStr}`);
const gsidv1CollisionStr = `${gsidv1Results.collisionRate.toFixed(8)}%`;
console.log(` GSID v1 (Prompt): ${gsidv1CollisionStr}`);
const gsidv2CollisionStr = `${gsidv2Results.collisionRate.toFixed(8)}%`;
console.log(` GSID v2 (Chat): ${gsidv2CollisionStr}`);
const idCollisionStr = `${idResults.collisionRate.toFixed(8)}%`;
console.log(` ID (By-example): ${idCollisionStr}`);
const simpleCollisionStr = `${simpleResults.collisionRate.toFixed(8)}%`;
console.log(` Simple (0-Simple): ${simpleCollisionStr}`);
const manualCollisionStr = `${manualResults.collisionRate.toFixed(8)}%`;
console.log(` GSID (5-Manual): ${manualCollisionStr}`);
const webCollisionStr = `${webResults.collisionRate.toFixed(8)}%`;
console.log(` GSID (5-Manual-Web): ${webCollisionStr}`);
const uuidCollisionStr = `${uuidResults.collisionRate.toFixed(8)}%`;
console.log(` UUID v4: ${uuidCollisionStr}`);
console.log('\n🌐 URL Safety:');
console.log(
` GSID (Tech-specs): ${gsidResults.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` GSID v1 (Prompt): ${gsidv1Results.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` GSID v2 (Chat): ${gsidv2Results.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` ID (By-example): ${idResults.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` Simple (0-Simple): ${simpleResults.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` GSID (5-Manual): ${manualResults.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` GSID (5-Manual-Web): ${webResults.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
console.log(
` UUID v4: ${uuidResults.isUrlSafe ? '✅ Safe' : '❌ Unsafe'}`,
);
const gsidTheoreticalEntropy = Math.log2(64) * gsidResults.avgSize;
const gsidv1TheoreticalEntropy = Math.log2(64) * gsidv1Results.avgSize;
const gsidv2TheoreticalEntropy = Math.log2(64) * gsidv2Results.avgSize;
const idTheoreticalEntropy = Math.log2(64) * idResults.avgSize;
const simpleTheoreticalEntropy = Math.log2(32) * simpleResults.avgSize;
const manualTheoreticalEntropy = Math.log2(64) * manualResults.avgSize;
const webTheoreticalEntropy = Math.log2(64) * webResults.avgSize;
const uuidTheoreticalEntropy = 122;
console.log('\n🧮 Theoretical Entropy:');
console.log(` GSID (Tech-specs): ${gsidTheoreticalEntropy} bits`);
console.log(` GSID v1 (Prompt): ${gsidv1TheoreticalEntropy} bits`);
console.log(` GSID v2 (Chat): ${gsidv2TheoreticalEntropy} bits`);
console.log(` ID (By-example): ${idTheoreticalEntropy} bits`);
console.log(` Simple (0-Simple): ${simpleTheoreticalEntropy} bits`);
console.log(` GSID (5-Manual): ${manualTheoreticalEntropy} bits`);
console.log(` GSID (5-Manual-Web): ${webTheoreticalEntropy} bits`);
const uuidSpec = `${uuidTheoreticalEntropy} bits (RFC 4122 specification)`;
console.log(` UUID v4: ${uuidSpec}`);
const theoreticalParams = [
gsidTheoreticalEntropy,
gsidv1TheoreticalEntropy,
gsidv2TheoreticalEntropy,
idTheoreticalEntropy,
simpleTheoreticalEntropy,
manualTheoreticalEntropy,
webTheoreticalEntropy,
uuidTheoreticalEntropy,
];
const bestTheoretical = Math.max(...theoreticalParams);
let bestName;
if (bestTheoretical === gsidTheoreticalEntropy) {
bestName = 'GSID (Tech-specs)';
} else if (bestTheoretical === gsidv1TheoreticalEntropy) {
bestName = 'GSID v1 (Prompt)';
} else if (bestTheoretical === gsidv2TheoreticalEntropy) {
bestName = 'GSID v2 (Chat)';
} else if (bestTheoretical === idTheoreticalEntropy) {
bestName = 'ID (By-example)';
} else if (bestTheoretical === simpleTheoreticalEntropy) {
bestName = 'Simple (0-Simple)';
} else if (bestTheoretical === manualTheoreticalEntropy) {
bestName = 'GSID (5-Manual)';
} else if (bestTheoretical === webTheoreticalEntropy) {
bestName = 'GSID (5-Manual-Web)';
} else {
bestName = 'UUID v4';
}
const bestTheoreticalPerf = `${bestTheoretical} bits`;
console.log(` Best theoretical: ${bestName} (${bestTheoreticalPerf})`);
console.log('\n💡 Key Advantages:');
const fastestAdvantage = `${fastest.toLocaleString()} IDs/sec`;
console.log(` ✅ Fastest: ${fastestName} (${fastestAdvantage})`);
const compactAdvantage = `${smallest.toFixed(1)} chars`;
console.log(` ✅ Most compact: ${smallestName} (${compactAdvantage})`);
const entropyAdvantage = `${bestEntropy.toFixed(4)} bits/char`;
console.log(` ✅ Best entropy: ${bestEntropyName} (${entropyAdvantage})`);
const theoreticalAdvantage = `${bestName} (${bestTheoreticalPerf})`;
console.log(` ✅ Best theoretical entropy: ${theoreticalAdvantage}`);
const allUrlSafe =
gsidResults.isUrlSafe &&
gsidv1Results.isUrlSafe &&
gsidv2Results.isUrlSafe &&
idResults.isUrlSafe &&
simpleResults.isUrlSafe &&
manualResults.isUrlSafe &&
webResults.isUrlSafe &&
uuidResults.isUrlSafe;
if (allUrlSafe) {
const urlSafeMsg = 'All implementations are URL-safe';
console.log(` ✅ ${urlSafeMsg}`);
}
const allNoCollisions =
gsidResults.collisionRate === 0 &&
gsidv1Results.collisionRate === 0 &&
gsidv2Results.collisionRate === 0 &&
idResults.collisionRate === 0 &&
simpleResults.collisionRate === 0 &&
manualResults.collisionRate === 0 &&
webResults.collisionRate === 0 &&
uuidResults.collisionRate === 0;
if (allNoCollisions) {
const msg = 'All implementations maintain excellent collision resistance';
console.log(` ✅ ${msg}`);
}
console.log('\n🎯 Recommended Use Cases:');
console.log(' 🚀 High-performance APIs: GSID');
console.log(' 🗄️ Database primary keys: GSID (shorter, faster)');
console.log(' 🔗 URL parameters: GSID (URL-safe, compact)');
console.log(' 🌍 General purpose: UUID v4 (widely supported)');
console.log(' 📅 Time-ordered data: Consider ULID or UUID v1');
console.log('\n✨ Benchmark completed successfully!');
};
runBenchmarks().catch(console.error);