The example code does not interact with network, so please don’t start prolonged discussion about local vs server scripts. There’s no remote communication so it can’t have influenced the result.
Roblox Lua runs over C++ which is why it is a bit slower than other programming languages. It’s like taking a path to several places, you start at home, then school, then back home. Roblox Lua gets compiled in C++ then the result is given back to us in Lua.
Unrelated:
Please add more content to your post. It does not follow the format for posting in this category. This would probably go in #discussion rather than scripting support as you are not seeking help with your code. Please read this post
The core of Roblox is programmed in I believe C++
Lua is a lightweight programming language which Roblox has adopted and integrated. Since Roblox is a game development platform and a business, they wanted an easily modifiable language. Now Roblox has done their best to increase the performance of their Lua engine and it has come a long way. If you compare a Roblox game to a steam game in performance, you’ll notice a MAJOR difference. One thing being that the Roblox client has to convert the Lua into whatever language the client application uses.
It ran in between 9.4s - 9.6s in both a server script and local script for me in studio.
You could probably drastically reduce the time by caching the values calculated. After I reduced it I could get around 1s to run the same script. This is probably another reason why js is much faster than Roblox Lua
You should compare it with LuaJIT. The most likely reason why Javascript is so much faster in your example is because you are using a javascript engine that is JIT-enabled, whereas the standard Lua VM / Luau that Roblox uses is not JIT-enabled.
(JIT = just-in-time compilation)
FYI this is not entirely accurate. Lua code gets compiled into Lua bytecode which is then interpreted by the standard Lua VM / Luau. Not transpiled to C++ code, or code in any other language.
local start = tick()
for i = 0, 16000000 do
local a = math.pow(8, 2)
local b = math.sqrt(64)
end
print("Done in: " .. tostring(tick() - start))
Done in: 7.0318927764893
let start = performance.now()
for (let i = 0; i < 16000000; i++){
let a = Math.pow(8, 2)
let b = Math.sqrt(64)
}
let stop = performance.now()
console.log("Done in: " + (stop - start) / 1000)
Done in: 4.033
Don’t know if I did this wrong, but that’s what my benchmarks looked like.
But to answer your question, it probably just has to do with the V8 engine being extremely fast - as well as compiling JavaScript into machine code at execution (JIT).