Is there significant overhead in repetitively calling C++ within v8 JavaScript?
Is there significant overhead in repetitively calling C++ within v8 JavaScript?
I wish to develop a JavaScript game engine that uses C++ as a back-end for rendering/updates/collision etc. Pretty much all the heavy lifting stuff.
There would then be C++ classes/functions that are exposed through modifying the isolate
variable (or maybe just a native nodejs module). Some of these classes, like the Sprite
class, could have its update
function overridden by a JS subclass in order to allow users to customize the behavior.
isolate
Sprite
update
Finally, the game engine would run in a loop within the JavaScript, but every frame would make a call to the C++ context to update/render and all the stuff PLUS there would be tons of calls to check input, collision, etc. Not to mention all the callbacks each subclass would make to the parent classes written in C++.
My concern is that I have read there is significant overhead (more than normal) when calling C++ from the JS context (be it ffi or native modules). Usually it's worth it for the performance, but considering how many calls would be made back and forth between the two languages each frame, perhaps this wouldn't be the best idea? Instead, maybe something like Python would be more appropriate due to its zero overhead (though Python in general is much slower), or a different JS interpreter all together?
@NeilButterworth Well, yeah, obviously, but certain scenarios can cause greater overhead than others, and I'm asking about this very specific case. A call from within C++ to a C++ or JS to a JS function is drastically different from JS to C++ within v8. I don't see why it's such a ridiculous question to ask in regards to it?
– Griffort
Aug 25 at 20:16
Python in general is slow. Perhaps not slower than JavaScript though :)
– zzxyz
Aug 25 at 20:18
@zzxyz CPython is without a doubt significantly slower than v8 JavaScript.
– Griffort
Aug 25 at 22:17
I wouldn't worry about it then @griffort it'll be insignificant
– Boinst
Aug 26 at 7:51
1 Answer
1
This answer is going to be very subjective, it's from observations from my experience that I wouldn't say are very rigorous, I'm working through this issue now myself, and i have not verified my claims with benchmarks. That said...
Yes, calling from JS to C++ is relatively expensive. Certainly more so than calls within pure JS. Substantially more so, in fact, than calls in the other direction, from C++ to JS. I assume that a major cause of the inefficiency is that the javascript engine loses some optimization opportunities.
However, assuming you stick with the V8 engine, calls from JS to C++ will be much faster than calling out into any other language.
Ahhh, interesting. From your knowledge, at least from a comparative perspective, would there be any other language/framework that would interact better with C++ as a front end (in terms of user-friendly scripting languages)? Or would this probably be the best choice overall?
– Griffort
Aug 26 at 7:54
@griffort V8 is a solid choice
– Boinst
Aug 26 at 8:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
All function calls, to whatever language, have an overhead.
– Neil Butterworth
Aug 25 at 19:48