A simple JavaScript library for quickly generating a random subset of arrays, iterators, or streams using reservoir sampling.
For Node.js, use npm
:
npm install reservoir
..then require
Reservoir:
var Reservoir = require('reservoir');
For the browser, add the following to your pages:
<script src="reservoir.js"></script>
...Or using AMD in the browser:
require(["reservoir"], function(Reservoir) {
// ...
});
A reservoir is just an array with one very special function added - pushSome
.
To create an empty reservoir that will contain a maximum of 3 randomly-chosen items:
var myReservoir = Reservoir(3);
Now, using that new reservoir we begin to add items into it:
var myData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
myData.forEach(function(e) {
myReservoir.pushSome(e);
});
At this point, myReservoir
will contain example 3 randomly-chosen values from the array myData
:
myReservoir => [2, 4, 7] // This can be any random subset of myData
As we can see, one important property of the Reservoir is that the order of the input is preserved.
Since, Reservoir.pushSome
operates much like the real Array.push
we can pass pushSome
more than one parameter and each one will be pushed in order. That means we can make the previous example more succinct by using Function.apply
:
myReservoir.pushSome.apply(myReservoir, myData);
Since the object returned from Reservoir() is just an array decorated with a pushSome
function, all normal array functions are available. Particularly of value are the iterator functions: forEach
, filter
, map
, every
, some
, reduce
, and reduceRight
.
TODO: Advanced usage (ie. see Some
).
reservoirSize
is the maximum size of the reservoir. This is the number of elements to be randomly chosen from the input provided to it using pushSome
.
randomNumberGenerator
is an optional random number generating function to use in place of the default Math.random
.
An empty Reservoir (an Array with the function pushSome
added to it).
datum
one or more elements to consider for inclusion into the reservoir.
The current length of the reservoir.
Copyright (C) 2012 Jon-Carlos Rivera
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.