Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active September 6, 2021 20:20
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolaj86/7e5ebb9a6708d0ebfc78 to your computer and use it in GitHub Desktop.
Save coolaj86/7e5ebb9a6708d0ebfc78 to your computer and use it in GitHub Desktop.
Fastest uuid generator for node.js

Now published as @root/uuid

This is started out as a curiosity, pasted as a gist but now, years later, I've come back to it because there are too many of these uuid modules out there and they're too complicated.

This, however, is easy to read and understand (for me, at least). Even its tests are dependncy and build-step free.

node-uuid vs crazy-uuid vs fast-uuid

Everybody knows about node-uuid and that it's pretty fast, right?

And everybody knows about 140byte-uuid and that it's insanely small, but really slow, right?

Well, while I was looking at all that math in node-uuid, I wondered to myself: Is that really all that more efficient than just accessing strings? (BTW, I was totally looking at the wrong function)

So I set out on a mission to make 'if' blocks and strings faster than bitwise operations, and I did! Over 2x faster! (generally in the range of 550,000/s instead of 250,000/s)

How is it 2x faster!?!?

Note that the huge time saver here is actually pooling crypto.randomBytes() ahead of time. If we were to implement the same thing in node-uuid we might see an even bigger increase.

License

Copyright 2021 The Root Group, LLC
Copyright 2015-2021 AJ ONeal

This Source Code Form is subject to the terms of the Mozilla
Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

'use strict';
var crypto = require('crypto');
var pool = 31 * 128; // 36 chars minus 4 dashes and 1 four
var r = crypto.randomBytes(pool);
var j = 0;
var str = "10000000-1000-4000-8000-100000000000";
var len = str.length; // 36
var strs = [];
strs.length = len;
strs[8] = '-';
strs[13] = '-';
strs[18] = '-';
strs[23] = '-';
function uuid(){
var ch;
var chi;
for (chi = 0; chi < len; chi++) {
ch = str[chi];
if ('-' === ch || '4' === ch) {
strs[chi] = ch;
continue;
}
// no idea why, but this is almost 4x slow if either
// the increment is moved below or the >= is changed to >
j++;
if (j >= r.length) {
r = crypto.randomBytes(pool);
j = 0;
}
if ('8' === ch) {
strs[chi] = (8 + r[j] % 4).toString(16);
continue;
}
strs[chi] = (r[j] % 16).toString(16);
}
return strs.join('');
}
module.exports = { v4: uuid };
'use strict';
var uuid = require('./fast-uuid');
// var uuid = require('node-uuid');
var now = Date.now();
var t = 0;
var msec;
for (t; t < 100000; t++) {
uuid.v4();
}
msec = Date.now() - now;
console.log(Math.floor((1000 / msec) * 100000) + ' uuid/s');
console.log('100000 in ' + (Date.now() - now) + 'ms');
console.log(uuid.v4());
@TJKoury
Copy link

TJKoury commented Sep 23, 2017

So, will this be published to NPM? Or is it already and I just suck at searching?

@devTechi
Copy link

Hi,
do have a license or something else, so that other people can use your gist snippet like: https://gist.github.com/martinbuberl/c0de29e623a1e34d1cda7e817d18bafe

Because your fast-uuid works very well. Thanks for that ☺️ 👍

@coolaj86
Copy link
Author

coolaj86 commented Jun 20, 2021

@TJKoury @devTechi A few years late, but I've finally published this to npm (and added a LICENSE).

@TJKoury
Copy link

TJKoury commented Jun 20, 2021

@coolaj86 Thanks. Per our last, more to come.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment