@@ -805,25 +805,21 @@ Comparison:
805
805
Hash#keys.each: 869262.3 i/s - 1.21x slower
806
806
```
807
807
808
- #### ` Hash#key? ` vs. ` Hash#[] ` vs. ` Hash#keys.include? ` [ code] ( code/hash/keys-include-vs-\[\] -vs-key.rb )
808
+ #### ` Hash#key? ` instead of ` Hash#keys.include? ` [ code] ( code/hash/keys-include-vs-\[\] -vs-key.rb )
809
809
810
810
> ` Hash#keys.include? ` allocates an array of keys and performs an O(n) search; <br >
811
- > ` Hash#key? ` performs an O(1) hash lookup without allocating a new array; <br >
812
- > ` Hash#[] ` performs an O(1) hash lookup as well.
811
+ > ` Hash#key? ` performs an O(1) hash lookup without allocating a new array.
813
812
814
813
```
815
- $ ruby -v code/hash/keys-include-vs-\[\]-vs-key.rb
816
- ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
814
+ $ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
817
815
818
816
Calculating -------------------------------------
819
- Hash#keys.include? 8.293k (± 6.1%) i/s - 41.964k in 5.083215s
820
- Hash#[] 6.412M (± 3.1%) i/s - 32.160M in 5.020295s
821
- Hash#key? 6.616M (± 5.0%) i/s - 33.178M in 5.030955s
817
+ Hash#keys.include? 8.612k (± 2.5%) i/s - 43.248k in 5.024749s
818
+ Hash#key? 6.366M (± 5.5%) i/s - 31.715M in 5.002276s
822
819
823
820
Comparison:
824
- Hash#key?: 6615589.2 i/s
825
- Hash#[]: 6412217.3 i/s - same-ish: difference falls within error
826
- Hash#keys.include?: 8293.2 i/s - 797.71x slower
821
+ Hash#key?: 6365855.5 i/s
822
+ Hash#keys.include?: 8612.4 i/s - 739.15x slower
827
823
```
828
824
829
825
##### ` Hash#value? ` instead of ` Hash#values.include? ` [ code] ( code/hash/values-include-vs-value.rb )
0 commit comments