Last active
June 2, 2021 15:08
-
-
Save kelgendy1204/27e9ed5343c9b6c4017322d1fbf5744c to your computer and use it in GitHub Desktop.
A/B testing 2 different approaches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createSamples(name, createVariation) { | |
const sampleCount = 10000; | |
const variations = []; | |
for (let i = 0; i < sampleCount; i++) { | |
variations.push(createVariation()); | |
} | |
const countData = variations.reduce((accum, curr) => { | |
if (accum[curr]) { | |
accum[curr]++; | |
} else { | |
accum[curr] = 1; | |
} | |
return accum; | |
}, {}); | |
const countPercent = Object.entries(countData).reduce((accum, curr) => { | |
const percentage = (curr[1] / sampleCount) * 100; | |
accum[curr[0]] = `${Math.round(percentage * 100) / 100}%`; | |
return accum; | |
}, {}); | |
console.group(name); | |
console.log(countPercent); | |
console.groupEnd(); | |
} | |
// ================ First approach ================ | |
function getRandomVariation1(experimentNeeded) { | |
const indexes = []; | |
for (let variationName in experimentNeeded) { | |
for (let i = 0; i < experimentNeeded[variationName]; i++) { | |
indexes.push(variationName); | |
} | |
} | |
const randomKey = indexes[Math.floor(Math.random() * indexes.length)]; | |
return randomKey; | |
} | |
const experimentNeeded = { | |
V1: 25, | |
V2: 25, | |
V3: 25, | |
V4: 25 | |
}; | |
// ================ Second approach ================ | |
const randomVariations = ['V1', 'V2', 'V3', 'V4']; | |
function getRandomVariation2(variations) { | |
function getRandomIntInclusive(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
return variations[getRandomIntInclusive(0, variations.length - 1)]; | |
} | |
// ================ Running experiments ================ | |
for (let i = 1; i < 6; i++) { | |
console.group(` | |
Experiment ${i}`); | |
createSamples('First Approach', () => getRandomVariation1(experimentNeeded)); | |
createSamples('Second Approach', () => getRandomVariation2(randomVariations)); | |
console.groupEnd(); | |
console.log('============================================='); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment