Performance.now() Function

Question:
I am making a Dice Roller program to see how long it takes to roll an amount of dice with an amount of sides. To start a timer I am using the performance.now() function but it doesn’t work, it says it’s not a function.

Repl Link:
https://replit.com/@SalladShooter/Die-Roller#index.js

var performance = require("performance");

function randInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

let dieAmount = prompt("Amount of Dice ")
let dieSides = prompt("Amount of Sides on a Die ")
dieSides = parseInt(dieSides)
dieAmount = parseInt(dieAmount)

let rolledCorrectly = false

let startTime = performance.now()

while (!rolledCorrectly) {
  const rolls = []
  
  for (let i = 0; i < dieAmount; i++) {
    let currentRoll = randInt(1, dieSides)
    rolls.push(currentRoll)
  }

  let allSame = true;
  for (let j = 1; j < rolls.length; j++) {
    if (rolls[0] !== rolls[j]) {
      allSame = false
      break
    }
  }

  if (allSame) {
    rolledCorrectly = true
  }
}

let endTime = performance.now()

let totalTime = (startTime - endTime) / 1000

console.log('Successfully rolled the same number on all dice! It took ${totalTime} seconds')

No, why… You know what const is…

Anyway do this

const { performance } = require("performance");

@QwertyQwerty88 I didn’t know how to import modules on JS so I looked it up (Im pretty new to JS), I do know how to use const’s for lists though. It does through another error though TypeError: Cannot read properties of undefined (reading ‘now’).

I don’t think your Node version is high enough for performance. Otherwise Idk what’s going on.

const { performance } = require("perf_hooks");
1 Like

Careful there, make sure you are using backticks, or it will raise an error. I’m guessing you’ve probably fixed this already.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.