crossbind
GitHub

The cost of a call

A call into native code costs more than a call inside it. Cross once with the whole job, not once for every item.

Both halves of main.js add the numbers from 1 to 50,000. The first calls add for each number; the second hands the loop to C++ with one call to sumTo. The work is the same, and the gap between the two timings is the cost of 50,000 crossings.

In a Web Worker each crossing is also a message to another thread, so the gap grows many times over. And native code is not faster by nature: for a small job the crossing can cost more than it saves, so measure, as this sample does.

Try it
Halve and double count, and watch how each timing follows.
Open in playground
Runs without a compile until the C++, Rust, packages or imports change.
Press Run, or Ctrl+Enter, to run main.js.

Your project

npm create crossbind@beta starts a project of your own. From there:

  • Quick start: crossbind in a new or an existing project.
  • C++ bindings: every type, pointers and buffers, constants and memory.
  • Rust and crates.io: Rust files and crates.
  • Libraries: ready-made C and C++ libraries, from SQLite to GDAL.
  • Apps: the same page and native code as a desktop and phone app.
  • Runtime API, Configuration and CLI: initNative(), crossbind.config.js and the command line.
  • Playground: every sample, free to change, with more packages.
Type to search every guide page and section.
↑↓ navigate↵ openesc close