Find string length 300 * slower
First I read the answer to the question Why is my WebAssembly function slower than the equivalent in JavaScript?
But it shed some light on this problem, and I spent a lot of time, which may very well be yellow on the wall.
I do not use globals, I do not use memory. I have two simple functions that find the length of a line and compare them to the same in plain old Javascript. I have 4 parameters and 3 more local ones, and I am returning a floating or double number.
In Chrome, Javascript is 40 times faster than webAssembly, and in Firefox wasm is almost 300 times slower than Javascript.
test case jsPref.
I added a test case in jsPref WebAssembly V Javascript math
What am I doing wrong?
Or
- I missed an obvious mistake, bad practice, or suffer from a stupid encoder.
- WebAssembly not for 32bit OS (win 10 i7CPU laptops)
- WebAssembly is far from finished technology.
Please select option 1.
I read an example of using web assembly
Reuse existing code with WebAssembly built into the larger JavaScript / HTML application. It can be anything from a simple helper library for offloading computationally oriented tasks.
I was hoping that I could replace some geometry libraries with webAssembly to get extra performance. I was hoping it would be cool, 10 or more times faster. BUT 300 times slower than WTF.
UPADTE
This is not a JS optimization problem.
To ensure that optimization has the smallest possible effect, I checked the following methods to reduce or eliminate any optimization errors.
- counter
c += length(... to make sure all code is executing. bigCount += c to ensure that the entire function is executed. Not required- 4 lines for each function to reduce tilt. Not required
- all values are randomly generated by double numbers
- each function call returns its result.
- add a slower length calculation to JS using
Math.hypot to prove that the code is executing. - added an empty call that returns the first JS parameter to see the overhead
Update Results.
Since the test has a lot more overhead, the results are closer, but the JS code is still two orders of magnitude faster.
Notice how slow the Math.hypo t function is. If optimization were to be performed, this function would be next to the faster len function.
- WebAssembly 13389 μs
- Javascript 728 μs
What is the point of WebAssambly if it does not optimize
End of update
All things related to the problem.
Find the length of the string.
Custom language source
The code compiles in Wat for reading
(module (func (export "lengthF") (param f32 f32 f32 f32) (result f32) (local f32 f32 f32) get_local 2 get_local 0 f32.sub set_local 4 get_local 3 get_local 1 f32.sub tee_local 5 get_local 5 f32.mul get_local 4 get_local 4 f32.mul f32.add f32.sqrt ) (func (export "length") (param f64 f64 f64 f64) (result f64) (local f64 f64 f64) get_local 2 get_local 0 f64.sub set_local 4 get_local 3 get_local 1 f64.sub tee_local 5 get_local 5 f64.mul get_local 4 get_local 4 f64.mul f64.add f64.sqrt ) )
Like compiled wasm in a hexadecimal string (Note does not include a name section) and is loaded using WebAssembly.compile. The exported functions are then run for the Javascript len function (in the following snippet)
And the test code is the same for all 3 functions and works in strict mode.
tests : [{ func : function (){ var i; for (i = 0; i < 100000; i += 1) { length(a1,a2,a3,a4); } }, name : "length64", },{ func : function (){ var i; for (i = 0; i < 100000; i += 1) { lengthF(a1,a2,a3,a4); } }, name : "length32", },{ func : function (){ var i; for (i = 0; i < 100000; i += 1) { len(a1,a2,a3,a4); } }, name : "lengthNative", } ]
FireFox Test Results