Last active
July 13, 2019 13:36
-
-
Save JoshCheek/585606c9349eca1d51d0b43d18bfdebe to your computer and use it in GitHub Desktop.
Ruby vs JS vs Lua (all nonlinear, but JS has best perf)
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
# 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