Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.