From 4995a4af211b7475c809a60fcd675c3b7157466d Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Fri, 19 Oct 2018 21:21:18 +0400 Subject: [PATCH] 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. --- app/controllers/github_repos_controller.rb | 4 ++-- app/liquid_tags/glitch_tag.rb | 2 +- app/services/suggester/users/recent.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/github_repos_controller.rb b/app/controllers/github_repos_controller.rb index 9265c1dd0..fc2668b02 100644 --- a/app/controllers/github_repos_controller.rb +++ b/app/controllers/github_repos_controller.rb @@ -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, diff --git a/app/liquid_tags/glitch_tag.rb b/app/liquid_tags/glitch_tag.rb index 42f38c061..6c98d9142 100644 --- a/app/liquid_tags/glitch_tag.rb +++ b/app/liquid_tags/glitch_tag.rb @@ -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] diff --git a/app/services/suggester/users/recent.rb b/app/services/suggester/users/recent.rb index b91fdc047..ab5a7f139 100644 --- a/app/services/suggester/users/recent.rb +++ b/app/services/suggester/users/recent.rb @@ -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