Skip to content

Instantly share code, notes, and snippets.

@fcheung
Created Jul 27, 2019
Embed
What would you like to do?
unpack cookie benchmark
require 'benchmark/ips'
def reverse(session_data)
digest, session_data = session_data.reverse.split('--', 2)
digest.reverse! if digest
session_data.reverse! if session_data
[session_data, digest]
end
def index(session_data)
index = session_data.rindex('--')
[session_data[0, index], session_data[index+2, session_data.length - (index+2)]]
end
def rpartition(session_data)
session_data, _, digest = session_data.rpartition('--')
end
n = 10
data = 'a'*512 + '--' + 'b'*32
Benchmark.ips do |x|
x.report('reverse:') { n.times { reverse(data) } }
x.report('index:') { n.times { index(data) } }
x.report('rpartition:') { n.times { rpartition(data) } }
end
reverse: 64.886k (± 5.2%) i/s - 328.547k in 5.079341s
index: 121.035k (± 1.8%) i/s - 607.898k in 5.024200s
rpartition: 129.777k (± 3.0%) i/s - 651.896k in 5.027945s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment