Permalink
Browse files

Optimized #stub_requests a bit.

Appending to an array with `<< value` is faster than with `+= [value]`, as it doesn't create a new array object for each append.

A benchmark shows there is about a 10x performance difference:

http://gist.github.com/544809
  • Loading branch information...
1 parent 8a3d22f commit 4d68e93e91901bc8e4c216e9d97533cf2560d64a @myronmarston committed Aug 23, 2010
Showing with 8 additions and 4 deletions.
  1. +4 −0 CHANGELOG.md
  2. +2 −2 lib/vcr/http_stubbing_adapters/fakeweb.rb
  3. +2 −2 lib/vcr/http_stubbing_adapters/webmock.rb
View
@@ -1,5 +1,9 @@
#Changelog
+## In Git
+
+* Optimized `VCR.http_stubbing_adapter.stub_requests` a bit.
+
## 1.1.0 (August 22, 2010)
* Added `:match_requests_on` cassette option, which determines how VCR matches requests.
@@ -23,10 +23,10 @@ def http_connections_allowed=(value)
def stub_requests(http_interactions, match_attributes)
validate_match_attributes(match_attributes)
- requests = Hash.new([])
+ requests = Hash.new { |h,k| h[k] = [] }
http_interactions.each do |i|
- requests[i.request.matcher(match_attributes)] += [i.response]
+ requests[i.request.matcher(match_attributes)] << i.response
end
requests.each do |request_matcher, responses|
@@ -22,10 +22,10 @@ def http_connections_allowed=(value)
end
def stub_requests(http_interactions, match_attributes)
- requests = Hash.new([])
+ requests = Hash.new { |h,k| h[k] = [] }
http_interactions.each do |i|
- requests[i.request.matcher(match_attributes)] += [i.response]
+ requests[i.request.matcher(match_attributes)] << i.response
end
requests.each do |request_matcher, responses|

0 comments on commit 4d68e93

Please sign in to comment.