My Journey in Open Source - ab-test-fn

cadienvan

Michael Di Prisco

Posted on January 9, 2023

My Journey in Open Source - ab-test-fn

Link to the repo

What is this?

A higher-order function to provide an A/B testing mechanism with multiple weights.

How do I install it?

npm install ab-test-fn
Enter fullscreen mode Exit fullscreen mode

How can I use it?

Use-case #1 - Simple A/B testing

In this scenario, we want to test two versions of a function, fnA and fnB, and we want to run fnA 80% of the time and fnB 20% of the time.

import { AB } from 'ab-test-fn';

const fnA = () => console.log('fnA');
const fnB = () => console.log('fnB');

const fnAB = AB(fnA, fnB, [0.8, 0.2]);

fnAB(); // Will log 'fnA' 80% of the time and 'fnB' 20% of the time
Enter fullscreen mode Exit fullscreen mode

Use-case #2 - A/B testing with multiple weights

In this scenario, we want to test three versions of a function, fnA, fnB and fnC, and we want to run fnA 50% of the time, fnB 30% of the time and fnC 20% of the time.

import { AB } from 'ab-test-fn';

const fnA = () => console.log('fnA');
const fnB = () => console.log('fnB');
const fnC = () => console.log('fnC');

const fnABC = AB(fnA, fnB, fnC, [0.5, 0.3, 0.2]);

fnABC(); // Will log 'fnA' 50% of the time, 'fnB' 30% of the time and 'fnC' 20% of the time
Enter fullscreen mode Exit fullscreen mode

API

The AB function accepts an indefinite number of functions and an array of weights. The weights can be either integers or floats, they don't have to add up to 1. The function will return a new function that will run one of the provided functions based on the sum of the weights.

Tests

You can run the tests by using the following command:

npm test
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
cadienvan
Michael Di Prisco

Posted on January 9, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

My Journey in Open Source - ab-test-fn
javascript My Journey in Open Source - ab-test-fn

January 9, 2023

My Journey in Open Source - hook-fn
javascript My Journey in Open Source - hook-fn

January 6, 2023

My Journey in Open Source - expirables
javascript My Journey in Open Source - expirables

December 29, 2022

My Journey in Open Source - key-value-cache
javascript My Journey in Open Source - key-value-cache

December 26, 2022