fermyon / spin-js-sdk

https://developer.fermyon.com/spin/javascript-components
Apache License 2.0
52 stars 18 forks source link

Performance of wasm binary seems less good than NodeJS #169

Closed tiagoernst closed 1 year ago

tiagoernst commented 1 year ago

Hey :wave: Really excited about this project and mainly for a question of better performance. I was trying to see how faster the code would run (from the compiled wasm binary) with a silly test:

let bef = new Date().getTime();
for (let i = 0; i < Math.pow(10, 7); i ++) {}
console.log('time passed', new Date().getTime() - bef);

This code seems to execute way faster with NodeJS REPL than with the wasm binary generated with spin-js-sdk (tried it locally and deployed, similar results). I know my test is probably not very good but I was wondering if you guys could explain a little bit why there is this difference ? I though since it was a compiled version of JS it would run faster

dicej commented 1 year ago

Hi @tiagoernst. You are correct that performance is not nearly as good as with NodeJS. spin-js-sdk is based on QuickJS, which is reasonably efficient as JS interpreters go but not nearly as efficient as a modern JIT-based JS runtime such as V8 (which is what NodeJS is based on). Here's a good comparison for reference: https://bellard.org/quickjs/bench.html.

spin-js-sdk does not compile JS to WebAssembly -- it merely bundles your JS code with the QuickJS interpreter, using the latter to interpret the former at runtime. Hence performance is no better than if you were to run your JS code in QuickJS natively.

There is ongoing work at Igalia and Fastly to improve JS-on-Wasm performance via certain AOT-compilation strategies, and we intend to adopt the results of that work when it's ready. Meanwhile, JS-on-Wasm is probably not a great choice for CPU-bound apps.

tiagoernst commented 1 year ago

Very clear! Thanks for the info 🙂