ccxvii / mujs

An embeddable Javascript interpreter in C.
http://mujs.com/
ISC License
813 stars 98 forks source link

A performance issue caused by loop. #173

Closed Ginni1110 closed 1 year ago

Ginni1110 commented 1 year ago

version:1.3.7

Overview:

If a method is called frequently in a loop of thousands of times, the execution time of mujs is longer than other engines. So what is the reason for this and can it be optimized.

TestCase:

var NISLFuzzingFunc = function (e) {
    for (; e > 180;)
        e -= 360;
    for (; e < -180;)
        e += 360;
};
var NISLParameter0 = 4875118.861784533;
for (var INDEX = 0; INDEX < 1000; INDEX++) {
    var NISLCallingResult = NISLFuzzingFunc(NISLParameter0);
}
print(NISLCallingResult);

Execution time:

mujs: 2186 ms

hermes: 96 ms

quickjs: 773 ms

ccxvii commented 1 year ago

Creating regular expressions allocates a new object each time. Hoist the regular expression creation outside the loop, and you may see a significant speed up.

Put this line outside the loop:

var re = /\t66EKZPb9ZjErsgZpMO8hrvs9gSOIrWb8a7OiNH6ylgnSovuXjKZGHyEDywxBzvzucleEdRWHhe0uLmWBoYvZ5SYdiShZcwrJSm4b?/i;

PS. Oh, you changed the example.

In the new example, there's nothing more to do. MuJS is sometimes just slower than other engines.

Ginni1110 commented 1 year ago

thank you~I want to know about the loop optimization part, does mujs have any plans to improve:)In addition, in the original example, if the regular expression definition is not placed outside the loop, has mujs considered optimization in this regard?