An Over-Engineered FizzBuzz Type in TypeScript

Regular FizzBuzz:

🥣 Basic Ingredients:

  • TypeScript 4.1
  • Numbers
  • Arithmetic
  • Loops
  • String Concatenation

🔢 Numbers

🧮 Arithmetic

🔁 Loops (Recursive)

📝 String Concatenation

🔪 Preparation

❓ Conditional Types

📏 Length

🎁 Unwrap

🏭 Tuple Factory

➕ Add

➖ Subtract

➗ Modulo

Let’s Bake this 🥐

  • FizzText will handle printing out “fizz”
  • BuzzText will handle printing out “buzz”
  • Coalesce will choose a given default if a given value is empty string ("")
  • FizzBuzzText will combine everything together

🎉 It works!

📈 Optimization

⏱ Results (in seconds):

╔══════╦══════════╦════════════╗
║ size ║ original ║ faster
╠══════╬══════════╬════════════╣
║ 500 ║ 29 ║ 3
║ 1000 ║ error ║ 9
║ 2000 ║ error ║ 33
║ 3000 ║ error ║ 56(error)
╚══════╩══════════╩════════════╝

🚅 Fast!

Linear Time

⏱ Results (in seconds):

╔══════╦══════════╦════════════╦═══════════╗
║ size ║ original ║ faster ║ fastest
╠══════╬══════════╬════════════╬═══════════╣
║ 500 ║ 29 ║ 3 ║ 1
║ 1000 ║ error ║ 9 ║ 3
║ 2000 ║ error ║ 33 ║ 14
║ 3000 ║ error ║ 56(error) ║ 41
╚══════╩══════════╩════════════╩═══════════╝

🚄 Faster!

--

--

Power using Typescript https://www.linkedin.com/in/steven-l-ab0a6a77/ https://github.com/whalemade

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store