mapbox / pixelmatch

The smallest, simplest and fastest JavaScript pixel-level image comparison library
ISC License
6.15k stars 305 forks source link

spread parameter is slow #105

Closed kawanet closed 3 months ago

kawanet commented 2 years ago

The small patch draws pixels up to x27 times faster.

The current code draws a pixel with spread parameters drawPixel(output, pos, ...color).

The magic ... looks cool but runs slower compared to traditional coding styles still.

code x 100,000,000 times Chrome 96 Safari 15 Firefox 95 result
drawPixel(output, pos, ...color) 1,333 ms 4,289 ms 5,230 ms slow 👎
drawPixel(output, pos, color[0], color[1], color[2]) 246 ms 1,552 ms 492 ms fast 👍
drawColorFn(output, color)(pos) 144 ms 1,154 ms 190 ms faster 👍 👍

The benchmark above was tested on MacBook M1 Apple Silicon. Try it: https://gist.github.com/kawanet/4676a1804bb463ea558017b063d4c286

mourner commented 3 months ago

Apologies for not noticing this PR at the time. 🙏 Closing in favor of a slightly simpler optimization which is fast enough.