Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove Hash#[] from tests
  • Loading branch information
JacobEvelyn committed May 2, 2018
commit 4b42f95e430a7888d341fa44e7bcd62c0cb377ac
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -805,25 +805,21 @@ Comparison:
Hash#keys.each: 869262.3 i/s - 1.21x slower
```

#### `Hash#key?` vs. `Hash#[]` vs. `Hash#keys.include?` [code](code/hash/keys-include-vs-\[\]-vs-key.rb)
#### `Hash#key?` instead of `Hash#keys.include?` [code](code/hash/keys-include-vs-\[\]-vs-key.rb)

> `Hash#keys.include?` allocates an array of keys and performs an O(n) search; <br>
> `Hash#key?` performs an O(1) hash lookup without allocating a new array; <br>
> `Hash#[]` performs an O(1) hash lookup as well.
> `Hash#key?` performs an O(1) hash lookup without allocating a new array.

```
$ ruby -v code/hash/keys-include-vs-\[\]-vs-key.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
$ ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

Calculating -------------------------------------
Hash#keys.include? 8.293k (± 6.1%) i/s - 41.964k in 5.083215s
Hash#[] 6.412M (± 3.1%) i/s - 32.160M in 5.020295s
Hash#key? 6.616M (± 5.0%) i/s - 33.178M in 5.030955s
Hash#keys.include? 8.612k (± 2.5%) i/s - 43.248k in 5.024749s
Hash#key? 6.366M (± 5.5%) i/s - 31.715M in 5.002276s

Comparison:
Hash#key?: 6615589.2 i/s
Hash#[]: 6412217.3 i/s - same-ish: difference falls within error
Hash#keys.include?: 8293.2 i/s - 797.71x slower
Hash#key?: 6365855.5 i/s
Hash#keys.include?: 8612.4 i/s - 739.15x slower
```

##### `Hash#value?` instead of `Hash#values.include?` [code](code/hash/values-include-vs-value.rb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ def key_fast
HASH.key? KEY
end

def key_resolve
!!HASH[KEY]
end

def key_slow
HASH.keys.include? KEY
end

Benchmark.ips do |x|
x.report("Hash#keys.include?") { key_slow }
x.report("Hash#[]") { key_resolve }
x.report("Hash#key?") { key_fast }
x.compare!
end