Why javascript binding's performance is so low?


#1

I created a project use cocos2d-x & js-binding, wrote it in the main.js

function testIt() {
    var max = 900;
    var start = new Date().getTime();
    for (var i = 0; i < max; ++i) {
        for (var j = 0; j < max; ++j) {}
    }
    cc.log("cost:"+(new Date().getTime() - start));
}
testIt();

It prints:

cost:821

but if I test it in the chrome’s developer tool,it only takes 9ms.
Is the cost of binding? Or where I made a mistake?


#2

yes, I met same problem. for the processing of an array with a lot of elements, it is very very slow.
I think chrome/nodejs V8 is realtime compiling and optimise a lot has a better performance than SpiderMonkey.
My solution is avoiding Intensive computing in javascript and using c++ do the Intensive computing then construct jsb to get the computing result


#3

Thanks for your reply, John! and It seems to be the only way can improve the performance.


#4

@yonghua,
Did you build your JS project in Release mode? Which platform did you test on?


#5

James Chen wrote:

@yonghua,
Did you build your JS project in Release mode? Which platform did you test on?

I tested on release mode ad hoc on ipod iphone, same result


#6

@James
No,I built it in Debug mode. As John said,There is no differences.


#7

Is it possible that SpiderMonkey is not optimizing empty arrays ?
And V8 detects that it is an empty array and it skip it?


#8

@Ricardo Quesada
Sorry,But if I do something in the loop,it also slow!


#9

Ricardo Quesada wrote:

Is it possible that SpiderMonkey is not optimizing empty arrays ?
And V8 detects that it is an empty array and it skip it?

father of cocos2d

glad to meet you:)

thanks for your great cocos2d


#10

yes, JS is slow when comes to looping, JIT helps, but not much, this is limited by the cpu power.

avoid heavy looping in JS logics, instead, offload heavy calculation to C++ function, and bind to it