crossbind
GitHub

Performance and size

Every build links only what your imports reach and compiles with production flags. Most apps need none of the settings below, and native code is faster only where a measurement says so.

What ends up in the binary

Libraries link statically, so only the parts your bindings reach ship. Release builds compile with -O3; dev servers serve debug builds, so measure size and speed on a production build.

The bindings decide what is reachable. From a dependency's header only what you import binds (C++), and library code that nothing you import reaches stays out:

src/main.js
import { GDALAllRegister, GDALOpenEx, GDALClose } from '@crossbind/port-gdal/gdal.h';

import * as, import(), require and a bare import 'x.h' bind the whole header. On this site's library demos (2.0.0-beta.63), binding only the imported functions shrank the wasm by 47% for GEOS, 40% for OpenSSL, 11% for zlib and 4% for GDAL.

Android ships libraries whole
crossbind builds Android libraries as shared libraries (.so), which keep everything they export: on Android, importing fewer functions trims the bindings, not the libraries.

Two more things add to what ships: the data files you declare, and export.wholeArchive: true, which links every archive whole (for libraries whose members register themselves from static initializers).

Apps

An app ships no browser engine. The sample app is 6.7 MB on an arm64 Mac, 1.8 MB zipped, 6 MB of it Hermes, the JavaScript engine the bindings run in; it shows its first native result 0.34 s after launch on an M-series Mac. Its Windows executable is 4.6 MB on x64, its iOS app 4.7 MB, and its Android release APK 14.8 MB with both ABIs, of which Google Play delivers one. Leaving out every default extension a plain app has saved 0.22 MB of 12.82 MB on macOS and 0.23 MB of 5.20 MB on Linux.

Speed: measure, do not assume

Native code is not automatically faster than JavaScript. Every call converts its arguments and results, and for a small function that can cost more than the work. What a call costs depends on what crosses. Measured in Node.js on an M-series Mac:

CallwasmNode-API addon
returns a short std::stringabout 160 nsabout 70 ns
takes a short std::stringabout 155 nsabout 85 ns
takes a const char*about 0.33 µsabout 1.2 µs
calls back into JavaScriptabout 0.5 µsabout 2.1 µs

Compute-bound runs went either way. Before moving work to native code:

  • Benchmark the real workload against the JavaScript you would otherwise ship, with representative inputs, on a production build.
  • Keep loops on the native side. One call that runs a whole pipeline crosses the boundary once; a call per element crosses it every time.
  • Count the worker hop. In a browser useWorker is on by default, so every call is also a message to the module's worker and back (Runtime API).
  • Profile with the browser's tools: the Performance tab for time spent in JavaScript and wasm, the Memory tab for the heap, and Lighthouse for the download, which counts toward the page's load metrics.

The defaults

Every wasm build gets these. If the app works, leave them.

Optimization
An -O flag of your own comes later and replaces it.
Default-O3 in release builds, none in debug
SIMD
On in every wasm build.
Default-msimd128
Exceptions
A C++ throw reaches JavaScript as an Error.
Default-fwasm-exceptions
Heap
Starts at Emscripten's initial size and grows on demand.
DefaultALLOW_MEMORY_GROWTH=1
Stack
A fixed size with no guard page.
Default64 KB, or 1 MB when the link includes Rust
Threads (mt builds)
At most two pthread workers; asking for more aborts.
Default-pthread, a pool of Math.min(navigator.hardwareConcurrency || 1, 2) workers, PTHREAD_POOL_SIZE_STRICT=2
Function table
Raise it on Cannot enlarge function table.
DefaultRESERVED_FUNCTION_POINTERS=200
Filesystem
m.FS, OPFS and data files rely on them.
DefaultFORCE_FILESYSTEM=1 in every build, WASMFS in browser and Node.js builds
No eval
The module runs under a strict Content Security Policy.
DefaultDYNAMIC_EXECUTION=0
Array buffers
Strings crossing the boundary need it in Firefox and WebKit.
DefaultGROWABLE_ARRAYBUFFERS=0

Safe overrides

Emscripten flags go in binary.emccFlags of a targetSpecs entry. Filter the entry by platform, arch, runtime, buildType or runtimeEnv so a flag reaches only the builds that need it.

crossbind.config.js
export default {
paths: { config: import.meta.url },
targetSpecs: [
{
platform: 'wasm',
specs: { binary: { emccFlags: ['-sINITIAL_MEMORY=64MB'] } },
},
],
};

Flags worth changing, and when:

-sINITIAL_MEMORY=64MB
Startup allocates a lot, such as a model or a large dataset, and you see pauses while the heap grows.
-sSTACK_SIZE=1MB
The C++ uses large local arrays or deep recursion. An overflow is silent in a release build: later, unrelated calls fail with unreachable or memory access out of bounds. Add -sSTACK_OVERFLOW_CHECK=2 to find the function.
-sPTHREAD_POOL_SIZE=8 with -sPTHREAD_POOL_SIZE_STRICT=1
An mt build measurably scales past two threads. Strict mode 1 warns and grows the pool instead of aborting.
-sRESERVED_FUNCTION_POINTERS=1024
A run fails with Cannot enlarge function table.

A wasm32 heap cannot pass 4 GB. For more, build wasm64: crossbind build -a wasm64 (a plain build skips it), or target: { arch: 'wasm64' } with a bundler plugin. Every flag crossbind sets is in performance.md.

What to leave alone

  • -Os or -Oz for a smaller file: an -O flag in emccFlags comes after the default -O3 and replaces it, trading speed for size. Measure both on your own binary before keeping it.
  • Turning off ALLOW_MEMORY_GROWTH for predictable allocation: the first allocation past the initial heap then fails with out of memory instead of growing.
  • A pool of 32 pthreads for parallelism: each pthread is a Web Worker with its own startup, runtime and stack (only the heap is shared), so raise the pool only as far as a measurement shows the work scales.
  • Flags that fight the defaults: the loader, m.FS, OPFS and data files depend on MODULARIZE, WASMFS and FORCE_FILESYSTEM; strict-CSP pages on DYNAMIC_EXECUTION=0; and C++ errors in JavaScript on -fwasm-exceptions (the standard library throws even when your own code does not).
Type to search every guide page and section.
↑↓ navigate↵ openesc close