Skip to content

Instantly share code, notes, and snippets.

@bajtos
Last active June 2, 2020 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bajtos/6343512b171156872d3b701b2c10a682 to your computer and use it in GitHub Desktop.
Save bajtos/6343512b171156872d3b701b2c10a682 to your computer and use it in GitHub Desktop.
Benchmark of generating unique IDs
== BEFORE ==
counter variable len: 24575-1
counter fixed length: 24575-0000000002
uuid v1: 60841ff0-a3f8-11ea-8da7-c5c2ea413f33
uuid v4: 767992e9-caa4-4e51-b694-2d7f0f85a7b1
hyperid variable len: D0TU/ybLT6u5DxndKGF2hg/0
hyperid fixed length: +OBh7XGXRsOViCS5XcghsQ/0000000000
== BENCHMARK ==
counter variable len: x 11,095,695 ops/sec ±1.50% (87 runs sampled)
counter fixed length: x 9,158,765 ops/sec ±1.46% (88 runs sampled)
uuid v1: x 1,649,816 ops/sec ±2.23% (86 runs sampled)
uuid v4: x 325,259 ops/sec ±2.37% (76 runs sampled)
hyperid variable len: x 10,234,508 ops/sec ±1.02% (90 runs sampled)
hyperid fixed length: x 8,720,125 ops/sec ±0.67% (91 runs sampled)
Fastest is counter variable len:
== AFTER ==
counter variable len: 24575-103923531
counter fixed length: 24575-0103923532
uuid v1: 74310d10-a3f8-11ea-8da7-c5c2ea413f33
uuid v4: ea1b09f4-b100-4a15-a006-e2382bd164b3
hyperid variable len: D0TU/ybLT6u5DxndKGF2hg/52338487
hyperid fixed length: +OBh7XGXRsOViCS5XcghsQ/0045333371
const Benchmark = require('benchmark');
const uuid = require('uuid');
const hyperid = require('hyperid');
const hyperidVarlen = hyperid({fixedLength: false});
const hyperidFixed = hyperid({fixedLength: true});
let counter = 1;
const pid = process.pid;
function generateVarlen() {
return pid + '-' + counter++;
}
function generatePadded() {
return pid + '-' + pad(counter++);
}
function pad (count) {
if (count < 10) return '000000000' + count
if (count < 100) return '00000000' + count
if (count < 1000) return '0000000' + count
if (count < 10000) return '000000' + count
if (count < 100000) return '00000' + count
if (count < 1000000) return '0000' + count
if (count < 10000000) return '000' + count
if (count < 100000000) return '00' + count
if (count < 1000000000) return '0' + count
return count
}
console.log('== BEFORE ==');
console.log('counter variable len:', generateVarlen());
console.log('counter fixed length:', generatePadded());
console.log(' uuid v1:', uuid.v1());
console.log(' uuid v4:', uuid.v4());
console.log('hyperid variable len:', hyperidVarlen());
console.log('hyperid fixed length:', hyperidFixed());
console.log('\n== BENCHMARK ==');
new Benchmark.Suite()
.add('counter variable len:', () => generateVarlen())
.add('counter fixed length:', () => generatePadded())
.add(' uuid v1:', () => uuid.v1())
.add(' uuid v4:', () => uuid.v4())
.add('hyperid variable len:', () => hyperidVarlen())
.add('hyperid fixed length:', () => hyperidFixed())
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
console.log('\n== AFTER ==');
console.log('counter variable len:', generateVarlen());
console.log('counter fixed length:', generatePadded());
console.log(' uuid v1:', uuid.v1());
console.log(' uuid v4:', uuid.v4());
console.log('hyperid variable len:', hyperidVarlen());
console.log('hyperid fixed length:', hyperidFixed());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment