Last active
August 1, 2017 22:53
-
-
Save niieani/225c590b6b7e92302e1a49925bc654c7 to your computer and use it in GitHub Desktop.
Benchmarking FizzBuzz implementations
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
const fizzers = { | |
'dummy': (n) => {throw new Error('reject first test')}, | |
'concat result': (n) => (n % 3 ? '' : 'fizz') + (n % 5 ? '' : 'buzz') || n, | |
'concat result strict': (n) => (n % 3 === 0 ? '' : 'fizz') + (n % 5 === 0 ? '' : 'buzz') || n, | |
'nested ternary': (n) => n % 3 | |
? (n % 5 ? n : "Buzz") | |
: (n % 5 ? "Fizz" : "FizzBuzz"), | |
'template string': (n) => `${n % 3 ? '' : 'fizz'}${n % 5 ? '' : 'buzz'}` || n, | |
'concat all': (n) => (n % 3 ? '' : 'fizz') + (n % 5 ? '' : 'buzz') + n, | |
'append string': (n) => { | |
let output = '' | |
if (!(n % 3)) output += 'Fizz' | |
if (!(n % 5)) output += 'Buzz' | |
return output || n | |
} | |
} | |
const tests = { | |
'do-while, Array[i] with size': (fizzer) => { | |
let result = new Array(100), n = 0 | |
do result[n] = fizzer(++n) | |
while (n < 100) | |
}, | |
'while, Array[i] with size': (fizzer) => { | |
let result = new Array(100), n = 0 | |
while (n < 100) result[n] = fizzer(++n) | |
}, | |
'while, Array[i] without size': (fizzer) => { | |
let result = [], n = 0 | |
while (n < 100) | |
result[n] = fizzer(++n) | |
}, | |
'while, Array.push': (fizzer) => { | |
let result = [], n = 0 | |
while (n < 100) | |
result.push( | |
fizzer(++n) | |
) | |
}, | |
'for, Array[i]': (fizzer) => { | |
let n, result = [] | |
for (n = 1; n < 101; n++) | |
result[n - 1] = fizzer(n) | |
}, | |
'for, Array.push': (fizzer) => { | |
let n, result = [] | |
for (n = 1; n < 101; n++) | |
result.push( | |
fizzer(n) | |
) | |
}, | |
'generator': (fizzer) => { | |
function *fizzbuzzerGenerator(n = 1, end = 100) { | |
do yield fizzer(n) | |
while (++n <= end) | |
} | |
const fizzbuzzer = fizzbuzzerGenerator() | |
const result = Array.from(fizzbuzzer) | |
}, | |
'Array.from': (fizzer) => { | |
const result = Array.from( | |
Array(100), | |
(_, i) => fizzer(i + 1), | |
) | |
}, | |
'Array.from length': (fizzer) => { | |
const result = Array.from( | |
{length: 100}, | |
(_, i) => fizzer(i + 1), | |
) | |
}, | |
'range-slice-map': (fizzer) => { | |
function range (start, length) { | |
return [...Array(start + length).keys()].slice(start) | |
} | |
const result = range(1, 100).map(fizzer) | |
}, | |
'range-push-map': (fizzer) => { | |
function range(start, length) { | |
let a = [start], b = start | |
while (--length) { | |
b++ | |
a.push(b) | |
} | |
return a | |
} | |
const result = range(1, 100).map(fizzer) | |
}, | |
} | |
const Benchmark = require('benchmark') | |
const suite = new Benchmark.Suite('fizz-buzz', { | |
onComplete: function() { | |
console.log(`Fastest is ${this.filter('fastest').map('name')}`) | |
}, | |
onCycle: function(event) { | |
const {target: {name, stats, times, count}} = event | |
console.log(event.target.name, count) | |
}, | |
maxTime: 5, | |
delay: 1, | |
}) | |
Object.entries(tests).forEach(([name, testFunction]) => { | |
Object.entries(fizzers).forEach(([fizzerName, fizzerFunction]) => { | |
suite.add(`${name} + ${fizzerName}`, () => testFunction(fizzerFunction)) | |
}) | |
}) | |
suite.run() |
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
{ | |
"name": "fizz-buzz-bench", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"benchmark": "2.1.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment