Shuffle (v2)

Revision 2 of this benchmark created on


Teardown

const array = Array.from({ length: 3000 }, () => Math.random());

shuffleArray(array)

Test runner

Ready to run.

Testing in
TestOps/sec
OLD
function shuffleArray(array) {
  const result = [...array]
  for (let i = result.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1))
    ;[result[i], result[j]] = [result[j], result[i]]
  }
  return result
}
ready
NEW
function shuffleArray (array) {
	const result = [...array]
    return result.sort((a, b) => 0.5 - Math.random());
}
ready

Revisions

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