Skip to content

Instantly share code, notes, and snippets.

@kasima
Created June 10, 2010 21:44
Show Gist options
  • Save kasima/433688 to your computer and use it in GitHub Desktop.
Save kasima/433688 to your computer and use it in GitHub Desktop.
Ruby #find_all vs V8 filter()
require 'rubygems'
require 'json'
require 'v8'
input = 'suggestions.txt'
suggestions = []
p "Loading suggestions..."
File.open(input, 'r').each { |line| suggestions << line.strip }
q = "beat"
cxt = V8::Context.new
cxt['suggestions'] = suggestions
cxt['q'] = q
start = Time.now
matches = suggestions.find_all{ |s| s.match(q) }
p "Ruby matches found: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
start = Time.now
matches = suggestions.find_all do |s|
cxt['suggestion'] = s
cxt.eval('suggestion.search(q)') > -1
end
p "V8/ruby #find_all matches found: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
start = Time.now
matches = Array(cxt.eval(<<-eojs
suggestions.filter(function(suggestion) {
return suggestion.search(q) > -1;
});
eojs
))
p "V8/js filter matches found: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment