We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
业务上的一个需求,要批量生成不重复的随机字符串。
先尝试 Google js generate many random strings,第一条结果就是 Stack Overflow 的,点进去看看:
js generate many random strings
Generate random string/characters in JavaScript
回答里面,有说用 Math.random() 方法的,有说用 Node.js 的 crypto 这个库的,还有说用 uuid 之类的库的。
Math.random()
crypto
uuid
看了这篇问答拿不定主意,于是再 Google node crypto randombytes duplicate,第一条结果是 node-hat 的 issue,里面有个高分回答也建议用 Node.js 的 crypto 这个库中的 randomBytes 方法。
node crypto randombytes duplicate
randomBytes
在 Google 出来的第二条结果 Secure random values (in Node.js) 中,也是既有人推荐用 Node.js 的 crypto 这个库(更加原生),也有人推荐用 uuid 之类的库(更能确保唯一性,但其实没啥区别)。
几个链接看下来,最后自己的结论就是:Node.js 的 crypto,和 uuid 这类的库都可以满足自己的需求。
另外,uuid 这个库只能以一种固定格式 1c572360-faca-11e7-83ee-9d836d45ff41 生成随机字符串,而 nanoid 这个库则可以自定义所生成的字符串的长度,可以自定义字符串中包含哪些字符,在自定义性上更胜一筹。
1c572360-faca-11e7-83ee-9d836d45ff41
略。
重要的事情有三点:
最后还是想说,GitHub 真是个宝库啊。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
需求描述
业务上的一个需求,要批量生成不重复的随机字符串。
方案调研
调研过程
先尝试 Google
js generate many random strings
,第一条结果就是 Stack Overflow 的,点进去看看:Generate random string/characters in JavaScript
回答里面,有说用
Math.random()
方法的,有说用 Node.js 的crypto
这个库的,还有说用uuid
之类的库的。看了这篇问答拿不定主意,于是再 Google
node crypto randombytes duplicate
,第一条结果是 node-hat 的 issue,里面有个高分回答也建议用 Node.js 的crypto
这个库中的randomBytes
方法。在 Google 出来的第二条结果 Secure random values (in Node.js) 中,也是既有人推荐用 Node.js 的
crypto
这个库(更加原生),也有人推荐用uuid
之类的库(更能确保唯一性,但其实没啥区别)。几个链接看下来,最后自己的结论就是:Node.js 的
crypto
,和uuid
这类的库都可以满足自己的需求。另外,uuid 这个库只能以一种固定格式
1c572360-faca-11e7-83ee-9d836d45ff41
生成随机字符串,而 nanoid 这个库则可以自定义所生成的字符串的长度,可以自定义字符串中包含哪些字符,在自定义性上更胜一筹。入选方案
排除方案
1c572360-faca-11e7-83ee-9d836d45ff41
生成随机字符串。应用过程
略。
要点总结
重要的事情有三点:
最后还是想说,GitHub 真是个宝库啊。
The text was updated successfully, but these errors were encountered: