npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lightmill/counterbalancing

v1.0.0

Published

Generate condition orders to counterbalance experiment runs.

Readme

@lightmill/counterbalancing

Decide the order in which each participant goes through the conditions of an experiment.

When every participant sees the conditions in the same order, learning and fatigue affect the last conditions more than the first ones. Counterbalancing varies the order across participants so these effects cancel out. This package generates the orders; you then give one to each participant.

Install

npm install @lightmill/counterbalancing

Example

import { latinSquare } from '@lightmill/counterbalancing';

const orders = latinSquare(['mouse', 'touch', 'pen'], { balanced: true });
// [
//   ['mouse', 'touch', 'pen'],
//   ['touch', 'pen', 'mouse'],
//   ['pen', 'mouse', 'touch'],
//   ['pen', 'touch', 'mouse'],
//   ['mouse', 'pen', 'touch'],
//   ['touch', 'mouse', 'pen'],
// ]

const order = orders[participantNumber % orders.length];

Every function returns a list of orders. Give participant i the order orders[i % orders.length]. Counterbalancing is complete when the number of participants is a multiple of orders.length.

Strategies

| Function | Orders | What it guarantees | | ---------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | latinSquare(conditions) | n | Each condition appears once at every position. | | latinSquare(conditions, { balanced: true }) | n or 2n | Also, each condition comes right before every other condition equally often. With an even number of conditions, the square is balanced anyway; with an odd number, this doubles the orders. | | permutations(conditions) | n! | Every possible order, once. | | randomOrders(conditions, { count, random? }) | count | Independent random orders. Nothing is guaranteed, but no order is favored. |

n is the number of conditions.

A balanced latin square also counters carryover effects: the effect one condition has on the condition right after it. Prefer it when such effects are likely, such as learning a technique that helps with the next one.

randomOrders uses Math.random by default. To get the same orders each time, for example to rebuild a participant's timeline when they resume, pass a seeded random number generator that returns numbers in [0, 1):

// mulberry32, a small seeded random number generator.
function seededRandom(seed: number) {
  return () => {
    let t = (seed += 0x6d2b79f5);
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

const [order] = randomOrders(conditions, {
  count: 1,
  random: seededRandom(participantNumber),
});

Every function throws a TypeError when conditions is empty or has duplicates, since the orders would not be counterbalanced. randomOrders throws a RangeError when count is not a non-negative integer.

Assigning orders to participants

The package does not know your participants: you choose which order each one gets. Number them from 0 or 1, for example through the experiment's URL, and use that number as the index.

Participants who drop out leave holes in the rotation. Fill them by giving a new participant the order of the one who dropped out. Keep the participant's identifier separate from the index that picks the order: the identifier usually names the participant's run, which can't be reused. For example, if participant 12 replaces participant 4:

const participantNumber = 12;
const orderIndex = 4;
const order = orders[orderIndex % orders.length];
const runName = `participant-${participantNumber}`;

Store the order index with the participant's identifier, so a resumed run gets the same order. If the replacement can't start a run, see Troubleshooting.

From orders to a timeline

An order is a list of conditions. Turn it into a timeline by expanding each condition into its tasks:

const order = latinSquare(['mouse', 'touch', 'pen'])[participantNumber % 3];

const timeline = order.flatMap((device) => [
  { type: 'instructions', id: `${device}-instructions`, device },
  ...Array.from({ length: 10 }, (_, i) => ({
    type: 'trial',
    id: `${device}-${i}`,
    device,
  })),
]);

With several factors, counterbalance the factor whose order matters, and nest the others inside each of its blocks, or counterbalance the combinations of their levels as conditions. For a factor that varies between participants, give each participant one level, again by participant number.