Use c++ functions in babylonjsNative

I have a c++ binary search method which is obviously faster than javascript, how do I implement this function in the solution and call it in experience.js.

1 Like

Pinging @bghgary, the BabylonNative architect and overlord.

In my experience, I’ve found The N-API Resource | to be a good resource in learning how to initialize NAPI functions with c++ implementation, and a more complex sample: The N-API Resource | ( is another good resource in learning the syntax for how NAPI passes data b/w javascript and c++.

I’m not sure if we have a great story in how to add your own NAPI-based additions to be ran from the javascript side, but hopefully this gets you started with the basics of using NAPI.

From there, you should be able to pass a reference of the javascript environment to your NAPI initialization logic, similarily to our sample application in how we setup the NAPI wrappers and environment modification for the other BabylonNative plugins on initialization:

After initialization, you should be able to call your functions as expected from your experience.js, as you’ve setup the javascript function definitions.

With all that out of the way, what are you hoping to speed up with a C++ search implementation? passing data to and from the javascript engine is the slowest part of these type of solutions, but there may be gains to be had if we use this properly.

1 Like

For application level stuff, I think Web assembly / Rust is a much better option. The current plug-ins have to be done this way, but you have a choice.

If you do it with NAPI, you will lose the ability to run in a browser. Even if you don’t plan to publish that way, having to do complete build for every single change is going to make you want to kill yourself.

For a scene I recently worked on, I probably had 500-600 runs over 3 months. To make it worse, about half those was developing pose detection for hand tracking. That meant a lot of headset on, then headset off. No way would I have been able pull that off without a browser.

1 Like

Honestly, I was considering using wasm port of my application but I’m yet to try it out…seems that now I have two options with what @Drigax mentioned


1 Like