projects blog

C call overhead is a lie

Posted: 2025-04-30

There is no such thing as overhead from calling native C functions: the loss in performance comes from the surrounding interpreted environment.

Calling a C function by pointer, from the executor's standpoint, is just as quick as evaluating an expression in the language in question; both actions are expressed in native code anyways.

But say, we'd like to draw an image pointwise by using a native putPixel procedure. The pure-native implementation would take less than a millisecond to run on decent hardware. The interpreted implementation would struggle for minutes before it outputs anything.

The amount of runtime the interpreter spends evaluating a nested loop and its scope boundaries, running garbage collection, boxing/unboxing dynamic values and performing type safety checks on them is orders of magnitude larger than how long the putPixel calls would take.

With that said, if you ever need to use a C library inside your JavaScript/Python project, consider whether you need JavaScript/Python at all.

Articles: