SOURCE

// 该方法不是真正的洗牌算法
// 数组个数少于十个的用的插入排序,多余十个就是用快速排序,无论哪种排序都会造成某些元素不会被交换
[].sort(a => Math.random() > .5);

// shuffle 算法优化比较 https://bost.ocks.org/mike/shuffle/
// 借鉴 lodash shuffle 方法
function shuffle(array) {
  let length = array == null ? 0 : array.length;
  if(!length) {
    return [];
  }
  let result = array.slice();
  let i = -1;
  while(++i < length){
    let index = i + Math.floor(Math.random() * (length - i));
    
    let temp = result[index];
    result[index] = result[i];
    result[i] = temp;
	}
  return result;
}

console.log(shuffle())
console.log(shuffle([1,2,3,4,5]))
console 命令行工具 X clear

                    
>
console