Created
June 10, 2010 21:44
-
-
Save kasima/433688 to your computer and use it in GitHub Desktop.
Ruby #find_all vs V8 filter()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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