I am currently working on an image editor and have stumbled upon this strange behavior regarding pixel manipulation and / or function calls in V8.
http://jsperf.com/canvas-pixelwise-manipulation-performance
There are two test cases. Both test cases should manipulate image data in a canvas in memory to increase brightness. Thus, they have to iterate over each pixel and manipulate the 4 color values ββof each pixel.
Case 1
Case 1 performs a β1 function call as a wholeβ, which means it passes the context and imageData to the function, which then iterates through the pixels and manipulates the data. All in one function
Case 2
Case 2 performs a β1 function call per pixel β, which means that it iterates over the pixels and calls a method for each pixel, which then manipulates the image data for that pixel. This results in (in this case) 250,000 additional function calls.
My expectation
I would expect case 1 to be much faster than case 2, since case 2 makes 250,000 extra function calls.
Result
In Chrome, it's quite the opposite. If I make 250,000 extra function calls, this is faster than a single function call processing all the images.
My question is: WHY?
Sascha gehlich
source share