Skip to content

e2tha-e/js-shuffle-algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

js-shuffle-algorithms

This repo is for comparing array shuffling algorithms:

  • Array.sort(() => Math.random() - 0.5)
  • Fisher-Yates

The scripts take the array [0, 1, 2] and shuffle it 10,000 times. The results show the time it took to execute, and the number of times each permutation resulted.

Sample results:

node js-sort-random.js

7 ms
012: 3735
021: 609
102: 1290
120: 615
201: 650
210: 3101

node fisher-yates.js

9 ms
012: 1694
021: 1636
102: 1687
120: 1633
201: 1682
210: 1668

While the Array.sort() + Math.random() algorithm is consistently faster, it is not significantly faster, and the poor randomness of the distribution is far too great to justify the time savings.

About

Comparison of array shuffling algorithms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published