Performance: replace ruby methods with their faster alternatives (#949)

- Array#select.first is slower than Array#detect.

  - Array#shuffle.first is slower than Array#sample.
This commit is contained in:
Nihad Abbasov 2018-10-19 21:21:18 +04:00 committed by Ben Halpern
parent 04968214ce
commit 4995a4af21
3 changed files with 5 additions and 5 deletions

View file

@ -34,9 +34,9 @@ class GithubReposController < ApplicationController
end
def fetched_repo_params
fetched_repo = @client.repositories.select do |repo|
fetched_repo = @client.repositories.detect do |repo|
repo.id == permitted_attributes(GithubRepo)[:github_id_code].to_i
end.first
end
{
github_id_code: fetched_repo.id,
user_id: current_user.id,

View file

@ -57,7 +57,7 @@ class GlitchTag < LiquidTagBase
params = options.map { |x| option_to_query_pair(x) }.compact
# Deal with the file option if present or use default
file_option = options.select { |x| x.start_with?("file=") }.first
file_option = options.detect { |x| x.start_with?("file=") }
path = file_option ? (file_option.sub! "file=", "") : "index.html"
params.push ["path", path]

View file

@ -9,10 +9,10 @@ module Suggester
def suggest
users = if user.decorate.cached_followed_tag_names.any?
((recent_producers(3) - [user]).
shuffle.first(55) + tagged_producers).uniq
sample(55) + tagged_producers).uniq
else
(recent_commenters(4, 30) + recent_top_producers - [user]).
uniq.shuffle.first(50)
uniq.sample(50)
end
users
end