Shuffle vs Select random

Benchmark created on


Description

FOR THE LOVE OF ALL THAT IS GOOD DO NOT SHUFFLE ARRAYS TO SELECT AN ITEM FROM IT.

Setup

function shuffle(a)
{
	var j, x, i;
	for (i = a.length;i;i--)
	{
		j = Math.floor(Math.random() * i);
		x = a[i - 1];
		a[i - 1] = a[j];
		a[j] = x;
	}
	return a;
}
function select_random(a) {
	return a[Math.floor(Math.random() * a.length)];
}
var RESULT = null;

Test runner

Ready to run.

Testing in
TestOps/sec
Shuffle
var moves = [
				[1, 0],
				[0, 1],
				[-1, 0],
				[0, -1],
				[0.8, 0.8],
				[-0.8, -0.8],
				[0.8, -0.8],
				[-0.8, 0.8],
			];
	shuffle(moves);
	RESULT = moves[0];		
ready
Select random
var moves = [
				[1, 0],
				[0, 1],
				[-1, 0],
				[0, -1],
				[0.8, 0.8],
				[-0.8, -0.8],
				[0.8, -0.8],
				[-0.8, 0.8],
			];
			RESULT = select_random(moves);
ready

Revisions

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