Skip to content

Instantly share code, notes, and snippets.

@brenolf
Created June 16, 2015 19:59
Show Gist options
  • Save brenolf/447ffa69f725a3bbec3a to your computer and use it in GitHub Desktop.
Save brenolf/447ffa69f725a3bbec3a to your computer and use it in GitHub Desktop.
Script used to generate this https://jsperf.com/switch-vs-hash/48
MAX = 1500
def stringfy(number)
text = ''
begin
remainder = number % 26
text += (remainder + 'a'.ord).chr
number = number / 26
end while number > 0
text
end
def preparation
puts "<script>
var theVar = 'sfc';
var cases = [];"
(0..MAX).each { |i| puts " cases['#{stringfy i}'] = function() {};" }
puts "</script>"
end
def eachgen
puts "switch (theVar) {"
(0..MAX).each do |i|
i = stringfy i
puts "case '#{i}':"
puts " cases['#{i}']();"
puts " break;"
end
puts "default:"
puts "}"
end
def ifelse
puts "if (theVar === 'a') {"
puts " cases['a']()"
print "}"
(1..MAX).each do |i|
i = stringfy i
print " else if (theVar === '#{i}') {\n"
puts " cases['#{i}']();"
print "} "
end
puts "else {}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment