Skip to content

Sample

Sampling is useful when you have a lot of data and it would be too costly in terms of execution speed to process each item. For example, perhaps a sensor is producing too much data to process in each frame of animation.

Throwing away data can of course impact precision, but depending on the source of data and how you’re working with it, this may not be a problem.

ixfx’s Arrays.sample can help.

import { Arrays } from 'https://unpkg.com/ixfx/dist/data.js';
const list = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
// Get 50% of input data
const sub1 = Arrays.sample(list, 0.5); // Yields: [2, 4, 6, 8, 10]

Or if a whole number is provided, it will return data of every x steps:

const sub2 = sample(list, 3); // Yields: [3, 6, 9]