Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Created January 16, 2014 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonRowe/8448064 to your computer and use it in GitHub Desktop.
Save JonRowe/8448064 to your computer and use it in GitHub Desktop.
TIL that `empty?` is still significantly faster than `any?` despite `any?` being lazy.
require 'benchmark'
array = 10000000.times.to_a
puts "Any"
puts Benchmark.measure { 1000.times { array.any? } }
puts "Empty"
puts Benchmark.measure { 1000.times { array.empty? } }
puts "Iterating over the entire collection"
puts Benchmark.measure { 1000.times { array.each {} } }
Any
0.000000 0.000000 0.000000 ( 0.000303)
Empty
0.000000 0.000000 0.000000 ( 0.000076)
Iterating over the entire collection
467.250000 0.150000 467.400000 (468.296172)
@puyo
Copy link

puyo commented Jan 16, 2014

Is it the extra logic cruft around detecting no block was passed, and deciding what to do about it... ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment