PERF: Optimize post editing

PERF: Optimize post editing

Reordering the conditions reduced the execution time by a factor of 5.

diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index d9647293f5..fbd9c012bf 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -359,7 +359,7 @@ class PostRevisor
     POST_TRACKED_FIELDS.each do |field|
-      if @fields.has_key?(field) || (should_create_new_version? && field == "edit_reason")
+      if @fields.has_key?(field) || (field == "edit_reason" && should_create_new_version?)
         @post.public_send("#{field}=", @fields[field])

GitHub sha: 8e27fa1b

I still think it should be faster if the test was outside the loop :wink:

cf. DEV: improve the code readability.

1 Like

I do not think so considering that POST_TRACKED_FIELDS will contain field = "edit_reason" at most once. :smiley:

Sure but weโ€™re doing the test field == "edit_reason" for each elements in the POST_TRACKED_FIELDS array whereas if it was outside the loop, weโ€™d do it only once :wink: