time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

_shuffleSelf.js (689B)


      1 var baseRandom = require('./_baseRandom');
      2 
      3 /**
      4  * A specialized version of `_.shuffle` which mutates and sets the size of `array`.
      5  *
      6  * @private
      7  * @param {Array} array The array to shuffle.
      8  * @param {number} [size=array.length] The size of `array`.
      9  * @returns {Array} Returns `array`.
     10  */
     11 function shuffleSelf(array, size) {
     12   var index = -1,
     13       length = array.length,
     14       lastIndex = length - 1;
     15 
     16   size = size === undefined ? length : size;
     17   while (++index < size) {
     18     var rand = baseRandom(index, lastIndex),
     19         value = array[rand];
     20 
     21     array[rand] = array[index];
     22     array[index] = value;
     23   }
     24   array.length = size;
     25   return array;
     26 }
     27 
     28 module.exports = shuffleSelf;