uuid4

JavaScript performance comparison

Revision 6 of this test case created by Navaru

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  (function(window, Math){
  
  function uuid4v1() {
  	var uuid = "", rand = Math.random, i, v;
  
    for (i = 0; i < 32; i++) {
      v = rand() * 16 | 0;
  
      if (i === 8 || i === 12 || i === 16 || i === 20) {
        uuid += '-';
      }
  
  		if (i === 12) {
  			uuid += '4';
  		} else {
  			uuid += (i === 16 ? (v & 3 | 8) : v).toString(16);
  		}
    }
  
    return uuid;
  }
  
  function uuid4v2() {
  	var uuid = [], rand = Math.random, i, v;
  
    for (i = 0; i < 32; i++) {
      v = rand() * 16 | 0;
  
      if (i === 8 || i === 12 || i === 16 || i === 20) {
        uuid.push('-');
      }
  
  		if (i === 12) {
  			uuid.push('4');
  		} else {
  			uuid.push((i === 16 ? (v & 3 | 8) : v).toString(16));
  		}
    }
  
    return uuid.join('');
  }
  
  function uuid4v3() {
  	var uuid = "", rand = Math.random, i, v;
  
    for (i = 0; i < 32; i++) {
      v = rand() * 16 | 0;
  
      if (i == 8 || i == 12 || i == 16 || i == 20) {
        uuid += '-';
      }
  
  		if (i == 12) {
  			uuid += '4';
  		} else {
  			uuid += (i == 16 ? (v & 3 | 8) : v).toString(16);
  		}
    }
  
    return uuid;
  }
  
  function uuid4v4() {
      var a = [], str, i;
      for (i = 0; i < 4; i++) {
          a.push(("0000000" + (Math.random()* 0x100000000 >>> 0).toString(16)).slice(-8));
      }
      str = a.join("");
      return str.slice(0, 8).concat(
          "-",
          str.slice(8, 12),
          "-4",
          str.slice(12, 15),
          "-",
          (8 + Math.random() * 4 | 0).toString(16),
          str.slice(15, 18),
          "-",
          str.slice(15,27)
      );
  }
  
  function uuid4v5() {
      var a = ("0000000" + ((Math.random() * 0x100000000 & 0xffff4fff | 0x4000) >>> 0).toString(16)).slice(-8),
          b = ("0000000" + ((Math.random() * 0x100000000 & 0xbfffffff | 0x80000000 ) >>> 0).toString(16)).slice(-8);
      return [
          ("0000000" + (Math.random() * 0x100000000 >>> 0).toString(16)).slice(-8),
          a.slice(0,4),
          a.slice(4),
          b.slice(0,4),
          b.slice(4) + ("0000000" + (Math.random() * 0x100000000 >>> 0).toString(16)).slice(-8)
      ].join("-");
  }
  
  function uuid4v6() {
      var r = Math.random,
          p = "0000000",
          n = 0x100000000,
          a = (p + ((r() * n & 0xffff4fff | 0x4000) >>> 0).toString(16)).slice(-8),
          b = (p + ((r() * n & 0xbfffffff | 0x80000000 ) >>> 0).toString(16)).slice(-8);
      return [
          (p + (r() * n >>> 0).toString(16)).slice(-8),
          a.slice(0,4),
          a.slice(4),
          b.slice(0,4),
          b.slice(4) + (p + (r() * n >>> 0).toString(16)).slice(-8)
      ].join("-");
  }
  
  var CHARS = '0123456789ABCDEF'.split(''),
      FORMAT = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.split(''),
      uuid4v7 = function (argument) {
      var c = CHARS, id = FORMAT, r;
  
      id[0] = c[(r = Math.random() * 0x100000000) & 0xf];
      id[1] = c[(r >>>= 4) & 0xf];
      id[2] = c[(r >>>= 4) & 0xf];
      id[3] = c[(r >>>= 4) & 0xf];
      id[4] = c[(r >>>= 4) & 0xf];
      id[5] = c[(r >>>= 4) & 0xf];
      id[6] = c[(r >>>= 4) & 0xf];
      id[7] = c[(r >>>= 4) & 0xf];
  
      id[9] = c[(r = Math.random() * 0x100000000) & 0xf];
      id[10] = c[(r >>>= 4) & 0xf];
      id[11] = c[(r >>>= 4) & 0xf];
      id[12] = c[(r >>>= 4) & 0xf];
      id[15] = c[(r >>>= 4) & 0xf];
      id[16] = c[(r >>>= 4) & 0xf];
      id[17] = c[(r >>>= 4) & 0xf];
  
      id[19] = c[(r = Math.random() * 0x100000000) & 0x3 | 0x8];
      id[20] = c[(r >>>= 4) & 0xf];
      id[21] = c[(r >>>= 4) & 0xf];
      id[22] = c[(r >>>= 4) & 0xf];
      id[24] = c[(r >>>= 4) & 0xf];
      id[25] = c[(r >>>= 4) & 0xf];
      id[26] = c[(r >>>= 4) & 0xf];
      id[27] = c[(r >>>= 4) & 0xf];
  
      id[28] = c[(r = Math.random() * 0x100000000) & 0xf];
      id[29] = c[(r >>>= 4) & 0xf];
      id[30] = c[(r >>>= 4) & 0xf];
      id[31] = c[(r >>>= 4) & 0xf];
      id[32] = c[(r >>>= 4) & 0xf];
      id[33] = c[(r >>>= 4) & 0xf];
      id[34] = c[(r >>>= 4) & 0xf];
      id[35] = c[(r >>>= 4) & 0xf];
  
      return id.join('');
  }
  
  
  window.uuid4v1 = uuid4v1;
  window.uuid4v2 = uuid4v2;
  window.uuid4v3 = uuid4v3;
  window.uuid4v4 = uuid4v4;
  window.uuid4v5 = uuid4v5;
  window.uuid4v6 = uuid4v6;
  window.uuid4v7 = uuid4v7;
  
  
  })(window, Math);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
uuid4v1
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v1());
pending…
uuid4v2
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v2());
pending…
uuid4v3
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v3());
pending…
uuid4v4
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v4());
pending…
uuid4v5
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v5());
pending…
uuid4v6
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v6());
pending…
uuid4v7
var arr = [];

for (var i = 0; i < 1000; i++) arr.push(uuid4v7());
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments