Why is Lua so slow?

JS

for(var i = 0; i < 16000000; i++){
	var a = Math.pow(8, 2);
  var b = Math.sqrt(64);
}

11.883056640625ms

Roblox LUA

for i = 0, 16000000 do
	  local a = math.pow(8, 2)
	  local b = math.sqrt(64)
	end

14.966703653336s! as “Script” and 8s as “LocalScript”

:confused:

9 Likes

13 posts were merged out

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

1 Like

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

2 Likes

My aim was to compare Identical scenarios. I know the script is slowly stupid, but that’s the intention :slight_smile:

1 Like

Are you using the new Luau VM? ROBLOX built their own Lua processor from scratch, and it is significantly faster than vanilla/old implementation.

You can read more about it and how to enable in Studio by clicking here.

3 Likes

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.

7 Likes
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).

1 Like

Heh, for me its 0.014874999993480742 in JS :slight_smile:

I can’t find that checkBox …

Yeah I tried it in Chrome and It’s significantly faster than Firefox (which is where I was getting 4 seconds from).

There might be a reason for that, but that’s beside the point.

1 Like

No new VM checkbox!??!?

1 Like

It’s under the file tab, just click “Beta features”.

I also just switched to that and the results were much more faster:

Done in: 0.73295569419861

2 Likes

Why is that option no in my List?, the screenshot above!

Maybe you need to scroll down?

Nope, its not there. I dont get it :confused:

Are you in the Roblox beta program?

1 Like

You mean I will get it after I do this Enroll in the Roblox Beta Program ?

Yes. You need to opt in to the roblox beta program.

1 Like