JS vs Lua Performance

Hello people,

why is the performance of JavaScript so bad/slow compared to Lua? Am I making a mistake or what is the reason?

Example:

I made a small script who trigger the ESX sharedobject and print it in the console.

The Script in LUA:

ESX = nil

Citizen.CreateThread(function()
    while not ESX do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

        Citizen.Wait(500)
    end
end)


Citizen.CreateThread(function()
    while true do
        print(ESX)
        Citizen.Wait(100)
    end
end)

The Script in JavaScript:

let ESX = null;

Wait = (ms) => new Promise(resolve =>  setTimeout(resolve, ms))

setTick(async () => {
    if (!ESX) {
        emit(`esx:getSharedObject`, obj => ESX = obj)
    }
    await Wait(500)
})


setTick(async () => {
    print(ESX)
    await Wait(100)
})

The Lua Script needs 0.01 - 0.02ms but the JavaScript needs 0.04 - 0.06ms and jumps at the start even to 0.70ms. When i replace all setTick with setInterval and remove all Wait functions. It needs even 0.06 - 0.07ms

Is JavaScript really so slow?

Hello did you find a solution for that problem ?

The Problem is that LUA is faster than JavaScript at least this is how it was described in other threads. Unfortunately, I have not found a correct solution. I have started to learn LUA which worked well and quickly.

For a start you’re using setTick which runs on every game tick without an exit clause and calling an event to get the huge ESX table, obviously this won’t be performant. And you’re talking a difference of 30-50 microseconds per tick which is negligible really. All things considered your choice shouldn’t be based on arbitrary resource monitor numbers, the quality of the code you write will have a bigger impact on server performance so just chose whichever language you prefer.

1 Like