RNG booleans

JavaScript performance comparison

Revision 2 of this test case created by Eli Grey

Preparation code

<script>
  (function(view) {

   "use strict";
  
   var
   aByte = view.Uint8Array && new Uint8Array(1),
       math_rand = Math.random,
       math_floor = Math.floor;
  
   view.new_byte_crypto_rand_bool = function() {
    var aByte = new Uint8Array(1);
    crypto.getRandomValues(aByte);
    return aByte[0] > 127;
   };
  
   view.old_byte_crypto_rand_bool = function() {
    crypto.getRandomValues(aByte);
    return aByte[0] > 127;
   };
  
   view.math_rand_floor_bool = function() {
    return !!math_floor(math_rand() * 2);
   };
  
   view.math_rand_OR_bool = function() {
    return !!((math_rand() * 2) | 0);
   };
  
  }(self));
</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
crypto.getRandomValues(new Uint8Array)
new_byte_crypto_rand_bool();
pending…
crypto.getRandomValues(aUint8Array)
old_byte_crypto_rand_bool();
pending…
Math.floor(Math.random()*2)
math_rand_floor_bool();
pending…
(Math.random()*2)|0
math_rand_OR_bool();
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