WebAssembly is “30X” Faster than JavaScript

Michael Bebenita
Mar 9, 2017 · 3 min read
int waCount(unsigned int x) {
int v = 0;
while(x != 0) {
x &= x - 1;
v++;
}
return v;
}
function jsCount(x) {
let v = 0;
while(x != 0) {
x &= x - 1;
v++;
}
return v;
}
let s = 0;
for (let i = 0; i < 100000; i++) {
s += xxCount(i);
}
JS       : 7.26ms
WA : 0.68ms
s = 0;
for (let i = 0; i < 100000; i++) {
let x = i;
while(x != 0) {
x &= x - 1;
s++;
}
}
int test(int c) {
int s = 0;
for (int i = 0; i < c; i++) {
s += count(i);
}
return s;
}
JS Inline: 1.13ms
WA Inline: 0.10ms

Don’t write tiny WebAssembly functions expecting them to be faster than JS. You’ll most likely be paying for the call overhead and that will outweigh whatever speed benefit you get from WebAssembly in the first place.

JS       : 2.57ms
JS Inline: 2.49ms
WA : 8.52ms
WA Inline: 0.09ms

Michael Bebenita

Written by

Researcher at Mozilla

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade