Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,22 @@ Comparison:
Array#insert: 0.2 i/s - 262.56x slower
```

#### `Enumerable#drop(1)` vs `Array#[1..]` [code](code/array/drop-vs-slice.rb)
```
ruby -v code/array/drop-vs-slice.rb
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Warming up --------------------------------------
Array#drop 831.515k i/100ms
Array#[] 536.131k i/100ms
Calculating -------------------------------------
Array#drop 8.306M (± 1.8%) i/s - 41.576M in 5.007158s
Array#[] 5.389M (± 2.4%) i/s - 27.343M in 5.076859s

Comparison:
Array#drop: 8305977.2 i/s
Array#[]: 5389130.2 i/s - 1.54x (± 0.00) slower
```

### Enumerable

##### `Enumerable#each + push` vs `Enumerable#map` [code](code/enumerable/each-push-vs-map.rb)
Expand Down
17 changes: 17 additions & 0 deletions code/array/drop-vs-slice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'benchmark/ips'

ARRAY = (1..100).to_a

def fast
ARRAY.drop(1)
end

def slow
ARRAY[1..]
end

Benchmark.ips do |x|
x.report('Array#drop') { fast }
x.report('Array#[]') { slow }
x.compare!
end