title: Javascript | Efficient loot drop probability
body: <p>I'm trying to get an efficient way of getting an item from a loot table (which can have hundreds of items), because it has to run on my node server and most online users will run it once every minute. So having an inefficient one could mean a lot of server costs. And I'm, of course, trying to avoid wasting resources.</p>
  <p>One way of doing it would be by filling an array by, if the chance is 10, then pushing 10 of that item.id into it and just pick one item from array with Math.random().</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const lootTable = [{
      id: 'stone_sword',
      chance: 500 // in 1000, which is then 50%
    },
    {
      id: 'gold_sword',
      chance: 100 // in 1000, which is then 10%
    },
    {
      id: 'iron_sword',
      chance: 100 // in 1000, which is then 10%
    },
    {
      id: 'diamond_sword',
      chance: 10 // in 1000, which is then 1%
    }
  ]
  
  let filledLootBox = []
  lootTable.forEach(item =&gt; {
    // inserts item.chance amount of times item.id into lootItem
    const lootItem = new Array(item.chance).fill(item.id)
  
    filledLootBox.push(...lootItem)
  })
  
  const pickedItem = filledLootBox[Math.floor(Math.random() * filledLootBox.length)]
  
  console.log('picked item:', pickedItem)
  console.log(filledLootBox)</code></pre>
  </div>
  </div>
  </p>
  <p>But, I just don't know how efficient this is in a server, where there's thousands of these requests a minute, because .fill seems to be sluggish/slow.</p>