-
Notifications
You must be signed in to change notification settings - Fork 377
/
keys-each-vs-each_key.rb
54 lines (49 loc) · 1.24 KB
/
keys-each-vs-each_key.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'benchmark/ips'
HASH = {
'provider' => 'facebook',
'uid' => '1234567',
'info' => {
'nickname' => 'jbloggs',
'email' => '[email protected]',
'name' => 'Joe Bloggs',
'first_name' => 'Joe',
'last_name' => 'Bloggs',
'image' => 'http://graph.facebook.com/1234567/picture?type=square',
'urls' => { 'Facebook' => 'http://www.facebook.com/jbloggs' },
'location' => 'Palo Alto, California',
'verified' => true
},
'credentials' => {
'token' => 'ABCDEF...',
'expires_at' => 1321747205,
'expires' => true
},
'extra' => {
'raw_info' => {
'id' => '1234567',
'name' => 'Joe Bloggs',
'first_name' => 'Joe',
'last_name' => 'Bloggs',
'link' => 'http://www.facebook.com/jbloggs',
'username' => 'jbloggs',
'location' => { 'id' => '123456789', 'name' => 'Palo Alto, California' },
'gender' => 'male',
'email' => '[email protected]',
'timezone' => -8,
'locale' => 'en_US',
'verified' => true,
'updated_time' => '2011-11-11T06:21:03+0000'
}
}
}
def slow
HASH.keys.each(&:to_sym)
end
def fast
HASH.each_key(&:to_sym)
end
Benchmark.ips do |x|
x.report('Hash#keys.each') { slow }
x.report('Hash#each_key') { fast }
x.compare!
end