RubyFlow The Ruby and Rails community linklog

Using [array].include? often, did you consider Set?

Writing [array].include? often? Do you know that in many cases Set[array].include? might be faster because it offers a constant look up time when array look up is linear - grows with the size of the array. Check out a simple benchmark results. And compare it to Java (if you’re interested)!

Comments

Good, but I want to note that writing code that way is not good at all. The thing that makes Ruby slow in general is the Garbage Collector. So creating a lot of objects will hurt performance. And your keeps creating new data every time you run it, although it’s a method that just checks the presence of an element in an Array. so needs_preview? method can be written like this

VALID_CONTENT_TYPES = %w{ application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document text/plain application/rtf image/jpeg }

VALID_EXTENTIONS = %w{.xls .xlsx .ppt .pptx .pdf .doc .docx .txt .rtf .jpg .jpeg}

def needs_preview? VALID_CONTENT_TYPES.include?(document_content_type) || VALID_EXTENSIONS.include?(extension) end this will create the arrays only once, so no garbage, and the GC will not need to run and the checks will be way faster.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in