FizzBuzz

Benchmark created on


Description

Как это можно переписать? https://www.youtube.com/watch?v=m7LmAdFI-QY https://www.youtube.com/live/MSLHs4z6sE4

Setup

const N = 100_000_000

Test runner

Ready to run.

Testing in
TestOps/sec
veged-1
function vFizzBuzz1(num) {
  const res = []

  for(let i1 = 1, i3 = 0, i5 = 0; i1 <= num; i1++) {
    if(i1 - i3 === 3) i3 = i1
    if(i1 - i5 === 5) i5 = i1

    let s = ''
    if(i1 === i3) s += 'Fizz'
    if(i1 === i5) s += 'Buzz'

    res.push(s || i1)
  }

  return res
}

vFizzBuzz1(N)
ready
veged-2
function vFizzBuzz2(num) {
  const res = []

  for(let i1 = 1, i3 = 0, i5 = 0; i1 <= num; i1++) {
    if(i1 - i3 === 3) i3 = i1
    if(i1 - i5 === 5) i5 = i1

    if(i1 === i3 && i1 === i5) res.push('FizzBuzz')
	else if(i1 === i3) res.push('Fizz')
	else if(i1 === i5) res.push('Buzz')
	else res.push(i1)
  }

  return res
}

vFizzBuzz2(N)
ready
victor-homyakov-1
function fizzbuzz1(n) {
	const res = [];
    for (let i = 1; i < n; i++) {
        const isFizz = i % 3 === 0;
        const isBuzz = i % 7 === 0;

        if (isFizz && isBuzz) {
            res.push('FizzBuzz');
        } else if (isFizz) {
            res.push('Fizz');
        } else if (isBuzz) {
            res.push('Buzz');
        } else {
            res.push(i);
        }
    }
	return res;
}

fizzbuzz1(N)
ready
victor-homyakov-2
function fizzbuzz2(n) {
    const result = [];
    let i = 1;
    while (i <= n - 21) {
        result.push(i, i + 1, 'Fizz', i + 3, i + 4, 'Fizz', 'Buzz', i + 7, 'Fizz', i + 9, i + 10, 'Fizz', i + 12, 'Buzz', 'Fizz', i + 15, i + 16, 'Fizz', i + 18, i + 19, 'FizzBuzz');
        i += 21;
    }

    while (i <= n) {
        if (i % 3 === 0 && i % 7 === 0) {
            result.push('FizzBuzz');
        } else if (i % 3 === 0) {
            result.push('Fizz');
        } else if (i % 7 === 0) {
            result.push('Buzz');
        } else {
            result.push(i);
        }
        i++;
    }
    return result;
}

fizzbuzz2(N);
ready
as-for-js
var typeFizzBuzz = (
	(theArrayLength) => {
		var theArr = (
			Array.from(
				{ length: theArrayLength }
				, (theVal, theIndex) => theIndex
			)
		);
		
		return (
			(theKvant, theName) => {
				for (let theIndex = theKvant; theIndex < theArrayLength; theIndex += theKvant) {
					theArr[theIndex] += theName
				}
				return theArr;
			}
		)
	}
)

var doFizzBuzz = typeFizzBuzz(N)
var theBuzz = doFizzBuzz(3, 'Fizz')
var theFinal = doFizzBuzz(7, 'Buzz')
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.