Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created July 4, 2013 07:34
Show Gist options
  • Save chakrit/5925648 to your computer and use it in GitHub Desktop.
Save chakrit/5925648 to your computer and use it in GitHub Desktop.
rx vs indexOf test
assert = require 'assert'
words = 'apple|orange|pear|banana|kiwi'.split '|'
strings =
noMatch : "The quick brown fox jumps over the lazy dog."
startMatch : "An apple is nice"
endMatch : "This is a longer sentence with the match for our fruit at the end: kiwi apple orange pear banana"
accept = (str) ->
for word in words
if (str.indexOf word) isnt -1
return true
return false
# comment out these blocks to test out indexOf search
rx = /(apple|orange|pear|banana|kiwi)/g
accept = (str) -> rx.test str
for i in [0..1000000]
for word in words
assert not accept strings.noMatch
assert accept strings.startMatch
assert accept strings.endMatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment