Created
June 2, 2018 03:05
-
-
Save bo33b/b568960328209820790a494a72003bc2 to your computer and use it in GitHub Desktop.
uuid one-liners (https://jsbench.github.io/#b568960328209820790a494a72003bc2) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>uuid one-liners</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
var uuid1 = (a=0,b='')=>{for(;a++<36;b+= ~a%5|a*3&4 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}; | |
var uuid2 = (a=0,b='')=>{for(;a++<36;b+= 4<<~a*6.5 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}; | |
var uuid3 = (a=0,b='')=>{for(;a++<36;b+= a*51&52 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}; | |
var uuid4 = (a=0,b='')=>{while(a++<36)b+= a*51&52 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-';return b}; | |
var uuid5 = (a=0,b='')=>{while(++a<37)b+= a*51&52 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-';return b}; | |
var uuid10 = ()=> [...Array(36)].reduce((a,b,c)=>a+= 4<<~c*6.5 ?(c^15?8^Math.random()*(c^20?16:4):4).toString(16):'-',''); | |
var uuid11 = ()=> [...Array(36)].reduce((a,b,c)=>a+= ++c*51&52 ?(c^15?8^Math.random()*(c^20?16:4):4).toString(16):'-',''); | |
var uuid12 = ()=> [...Array(36).keys()].map(a=> 4<<~a*6.5 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-').join(''); | |
var uuid13 = ()=> [...Array(36).keys()].map(a=> ++a*51&52 ?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-').join(''); | |
var uuid20 = a=> a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, uuid20 ); | |
}; | |
suite.add("uuid3();", function () { | |
uuid3(); | |
}); | |
suite.add("uuid4();", function () { | |
uuid4(); | |
}); | |
suite.add("uuid5();", function () { | |
uuid5(); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("uuid one-liners"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment