Skip to content

Instantly share code, notes, and snippets.

@icebob
Forked from gobwas/index.html
Last active January 10, 2017 10:46
Show Gist options
  • Select an option

  • Save icebob/940eea0410f245cb9d3b11d3bd0d4cb1 to your computer and use it in GitHub Desktop.

Select an option

Save icebob/940eea0410f245cb9d3b11d3bd0d4cb1 to your computer and use it in GitHub Desktop.

Revisions

  1. icebob revised this gist Jan 10, 2017. 1 changed file with 0 additions and 20 deletions.
    20 changes: 0 additions & 20 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -22,26 +22,6 @@
    });
    });

    suite.add("function random(min, max) {", function () {
    function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/(x{1,8}|y)/g, function(c) {
    var r, v;
    var n = c.length;

    if (n == 1) {
    r = Math.random()*16|0;
    v = c == 'x' ? r : (r&0x3|0x8);
    } else {
    v = random(1<<(4*(n-1)), (1<<(4*n)) - 1)
    }

    return v.toString(16);
    });
    });

    suite.add("const d0 = Math.random()*0xffffffff|0;", function () {
    const d0 = Math.random()*0xffffffff|0;
    const d1 = Math.random()*0xffffffff|0;
  2. icebob revised this gist Jan 10, 2017. No changes.
  3. icebob revised this gist Jan 10, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,10 @@
    })(function (Benchmark) {
    var suite = new Benchmark.Suite;

    Benchmark.prototype.setup = function () {
    const lut = [];
    for (let i=0; i<256; i++) { lut[i] = (i<16?'0':'')+(i).toString(16); }
    };


    suite.add("'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {", function () {
    @@ -38,6 +42,17 @@
    });
    });

    suite.add("const d0 = Math.random()*0xffffffff|0;", function () {
    const d0 = Math.random()*0xffffffff|0;
    const d1 = Math.random()*0xffffffff|0;
    const d2 = Math.random()*0xffffffff|0;
    const d3 = Math.random()*0xffffffff|0;
    return lut[d0&0xff]+lut[d0>>8&0xff]+lut[d0>>16&0xff]+lut[d0>>24&0xff]+'-'+
    lut[d1&0xff]+lut[d1>>8&0xff]+'-'+lut[d1>>16&0x0f|0x40]+lut[d1>>24&0xff]+'-'+
    lut[d2&0x3f|0x80]+lut[d2>>8&0xff]+'-'+lut[d2>>16&0xff]+lut[d2>>24&0xff]+
    lut[d3&0xff]+lut[d3>>8&0xff]+lut[d3>>16&0xff]+lut[d3>>24&0xff];
    });

    suite.on("cycle", function (evt) {
    console.log(" - " + evt.target);
    });
  4. @gobwas gobwas created this gist Dec 23, 2015.
    13 changes: 13 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <title>UUID</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>
    60 changes: 60 additions & 0 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    "use strict";

    (function (factory) {
    if (typeof Benchmark !== "undefined") {
    factory(Benchmark);
    } else {
    factory(require("benchmark"));
    }
    })(function (Benchmark) {
    var suite = new Benchmark.Suite;



    suite.add("'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {", function () {
    'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
    });
    });

    suite.add("function random(min, max) {", function () {
    function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/(x{1,8}|y)/g, function(c) {
    var r, v;
    var n = c.length;

    if (n == 1) {
    r = Math.random()*16|0;
    v = c == 'x' ? r : (r&0x3|0x8);
    } else {
    v = random(1<<(4*(n-1)), (1<<(4*n)) - 1)
    }

    return v.toString(16);
    });
    });

    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");
    console.log(new Array(30).join("-"));
    suite.run();
    });