Fisher-Yates shuffle for immutable.js
Immutable.js is an awesome, but does not provide a way to shuffle lists out of the box.
This module implements Fisher-Yates Shuffle for immutable.js. Some say it is the only way to shuffle an array in JavaScript.
npm install immutable-shuffle
import shuffle from 'immutable-shuffle';
const list = Immutable.fromJS([1, 2, 3, 4, 5]);
const shuffled = shuffle(list);
A naive shuffle could be implemented as follows:
const shuffled = list.sortBy(Math.random);
Compared to a fisher-yates shuffle this implementation is much slower.
On a Macbook Air 2013:
array of 100 elements
naive: 2.503ms
fisher-yates: 0.568ms
array of 1000 elements
naive: 10.218ms
fisher-yates: 4.500ms
array of 10000 elements
naive: 30.075ms
fisher-yates: 13.920ms
array of 100000 elements
naive: 357.804ms
fisher-yates: 84.567ms
array of 1000000 elements
naive: 2877.592ms
fisher-yates: 833.101ms
PRs are very welcome.
Small note: If editing the README, please conform to the standard-readme specification.
MIT © Tobias Kloht