Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active July 13, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/585606c9349eca1d51d0b43d18bfdebe to your computer and use it in GitHub Desktop.
Save JoshCheek/585606c9349eca1d51d0b43d18bfdebe to your computer and use it in GitHub Desktop.
Ruby vs JS vs Lua (all nonlinear, but JS has best perf)
# Code from this tweet: https://twitter.com/josh_cheek/status/1150035017319497728
$ node -e '
const n = 10000
const str = "x=" + "x".repeat(n)
const regex = /.*.*=.*/
const label = `n=${n}`
console.time(label)
const match = str.match(regex)
console.timeEnd(label)
console.log(" ", { match: !!match })
'
ruby -e '
n = 10_000
str = "x=" + "x"*n
regex = /.*.*=.*/
start = Time.now
match = str.match? regex
dur = Time.now - start
p n: n, match: match, seconds: dur
'
lua -e '
local n = 10000
local str = "x=" .. string.rep("x", n)
local regex = ".*.*=.*"
local start = os.clock()
local match = string.match(str, regex)
print("n="..n, os.clock()-start, match ~= nil)
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment