shuffle (v2)

Revision 2 of this benchmark created on


Setup

const array = [...Array(1000).keys()];

Test runner

Ready to run.

Testing in
TestOps/sec
shufffle0
  for (let i = array.length - 1; i >= 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
ready
shuffle1
  for (let i = array.length - 1; i >= 1; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
ready
shuffle2
  for (let i = array.length; i >= 2; ) {
    const j = Math.floor(Math.random() * i--);
    [array[i], array[j]] = [array[j], array[i]];
  }
ready
shuffle3
  for (let i = array.length; i; ) {
    const j = Math.floor(Math.random() * i--);
    [array[i], array[j]] = [array[j], array[i]];
  }
ready
shuffle2a
  for (let i = array.length; i > 1; ) {
    const j = Math.floor(Math.random() * i--);
    [array[i], array[j]] = [array[j], array[i]];
  }
ready

Revisions

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