Fundamentals 10 min read

Performance Evaluation of JavaScript, Lua, and Native Java for Dynamic Scripting

The study compares JavaScript (via the JVM engine), Lua, and pure Java for secure, lightweight dynamic scripting in Java projects, finding that native Java is orders of magnitude faster, JavaScript outperforms Lua in complex loops when the engine is reused, while Lua remains suitable for simple scripts but all scripting options are significantly slower than compiled Java.

DaTaobao Tech
DaTaobao Tech
DaTaobao Tech
Performance Evaluation of JavaScript, Lua, and Native Java for Dynamic Scripting

Background: The author needed a dynamic‑script solution for Java projects that satisfies security, ease‑of‑use, and high‑performance requirements.

Requirements: (1) Simple logical constructs (if, for loops); (2) Strong security – no I/O, object creation, or heavy memory usage; (3) Acceptable performance under high concurrency.

Selected candidates for comparison: JavaScript (native engine), Lua (lightweight), and pure Java code.

Test 1 – Million‑iteration loop with accumulation

function test(){
  var a=0;
  for(i=0; i<=10000000; i++){
    if(a<i){
      a++;
    };
  };
  return a;
}
a = 0
for i=0,10000000,1 do
  if(i > a) then
    a=a + 1
  end
end
return a
int a = 0;
for(int i = 0;i<=10000000;i++){
  if(i > a){
    a++;
  }
}

Average execution time (ms): JavaScript ≈ 690, Lua ≈ 1440, Native Java ≈ 9.6.

Test 2 – Adding a character each iteration

JavaScript and Java performed the loop, while Lua timed out.

Average execution time (ms): JavaScript ≈ 1837, Native Java ≈ 129.

Test 3 – Reduced loop (100 k iterations)

Average execution time (ms): JavaScript ≈ 585, Lua ≈ 1296, Native Java ≈ 6.5.

Test 4 – Engine initialization overhead (10 k script calls)

public static void main(String[] args) throws Exception {
  long now = System.currentTimeMillis();
  for(int i = 0; i<10000;i++){
      jsScript();
  }
  System.out.println(System.currentTimeMillis() - now);
}

private static void jsScript() throws Exception {
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByExtension("js");
        engine.eval("function test(){var a=0;for(i=0;i<=10;i++){ if(a<i){a++;};}; return a}");
        Invocable inv = (Invocable) engine;
        String value = String.valueOf(inv.invokeFunction("test"));
}

Average execution time (ms): JavaScript ≈ 18848, Lua ≈ 1425, Native Java ≈ 1.

Test 5 – Small loop (10 k iterations)

Average execution time (ms): JavaScript ≈ 3367, Lua ≈ 331, Native Java ≈ 1.

Conclusions:

Never recreate the script engine for each call; reuse the engine to avoid 5‑6× overhead.

For complex scripts with many loops and string operations, JavaScript is preferable because it compiles to JVM bytecode.

For simple, lightweight scripts, Lua offers lower resource consumption.

All scripting solutions are at least an order of magnitude slower than pure Java.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformanceJavaScriptBenchmarkScriptingLua
DaTaobao Tech
Written by

DaTaobao Tech

Official account of DaTaobao Technology

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.