Re: miniSphere 4.8.6
Reply #2109 –
If I'm going to hazard a guess, I'd surmise that crossing the JS<->C boundary is inherently expensive. In Duktape a native call is super cheap; in fact, other things being equal, it's probably faster to call an API function than an equivalent JS function just because there's less overhead. Duktape has to set up a stack frame and push the parameters (and then pop them on return), but that's it, otherwise it's just a straight C function call. A JS call involves a lot more bookkeeping including setting up a new bytecode executor instance, etc.
In an engine like Chakra the story is different: JS is a very dynamic language and optimizations rely on making educated guesses about what the code is going to do based on things like static analysis, observation of the code at runtime, what have you. Once you call into native code though, all bets are off, the call is essentially a black box from the point of view of the engine and pretty much anything could happen. JS values might be modified, types changed in unexpected ways, etc.
So yeah, my hypothesis is that making so many API calls causes a lot of optimizations and/or JIT'd code to be thrown away, which is expensive for the much the same reason as a branch prediction failure is expensive on a modern CPU.