Ways to get a random string

JavaScript performance comparison

Test case created by cfv1984

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
Array.from for iteration
var dictionary = 'abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ0123456789-';
var len = 10000;
var random = Math.random;
var floor  = Math.floor;

function getRandomChar(){
  return dictionary[floor(random() * dictionary.length)];
}

var result = Array.from(Array(len), getRandomChar).join('');
pending…
Batch random.toString()
var buffer = "";
var len = 10000;

while(buffer.length < len){
 buffer += Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0,5);
}

var result = buffer.substring(0, len);
pending…
While loop for iteration
var dictionary = 'abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ0123456789-';
var buffer = '';
var len = 10000;
var random = Math.random;
var floor  = Math.floor;

while(buffer.length < len){
  buffer += dictionary[floor(random() * dictionary.length)];
}
var result = buffer.substring(0, len);
pending…

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

Compare results of other browsers

0 Comments