Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Last active March 16, 2024 15:14
Show Gist options
  • Save SimonHoiberg/ad2710c8626c5a74cddd8f6385795cc0 to your computer and use it in GitHub Desktop.
Save SimonHoiberg/ad2710c8626c5a74cddd8f6385795cc0 to your computer and use it in GitHub Desktop.
Create a unique string
const uid = () => {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
};
// Usage. Example, id = khhry2hb7uip12rj2iu
const id = uid();
@depoulo
Copy link

depoulo commented Nov 24, 2020

You guys sure know about this one? https://gist.github.com/jed/982883

@andreasmischke
Copy link

You guys sure know about this one? https://gist.github.com/jed/982883

Sure, but this here is still nice for smaller use cases and 87% faster on my machine: https://jsbench.me/ovkhvq1uct/1

@fernandocanizo
Copy link

how about using performance.now() instead of Date.now()? It should reduce the probability of collision further.

It will only work on the web, not under Nodejs

@ashleyfrieze
Copy link

Seriously...

https://www.npmjs.com/package/uuid

import { v4 as uuidv4 } from 'uuid';

const myKey = uuidv4();

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