Browse Source

[ruby/rails] Reduce `random_id` calls in update (#8856)

Calling `Array#sample` with a size x is faster than calling `rand` x
times.
Petrik de Heus 1 year ago
parent
commit
f1c8109c74
1 changed files with 2 additions and 2 deletions
  1. 2 2
      frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

+ 2 - 2
frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

@@ -33,8 +33,8 @@ class HelloWorldController < ApplicationController
   end
 
   def update
-    worlds = Array.new(query_count) do
-      world = World.find(random_id)
+    worlds = ALL_IDS.sample(query_count).map do |id|
+      world = World.find(id)
       new_value = random_id
       new_value = random_id until new_value != world.randomNumber
       world.update_columns(randomNumber: new_value)