zschuessler / DeltaE

CIE color difference formulas in JavaScript
http://zschuessler.github.io/DeltaE
The Unlicense
260 stars 25 forks source link

One obvious speedup opprotunity #2

Closed adishavit closed 7 years ago

adishavit commented 7 years ago

Since all you are doing is thresholding the distance relative to several reference points, you can forgo the sqrt() and just square the thresholds.
sqrt() is a a very expensive function - avoiding it may significantly improve performance.

zschuessler commented 7 years ago

The performance difference in Chrome latest stable is 3%. The JiT compiler has gotten very good at optimizing math functions and storing shadow variables!

Used jsPerf for this simplified formula, compared to current DeltaE implementation:

return(x2.L - x1.L) * (x2.L - x1.L) + (x2.A - x1.A) * (x2.A - x1.A) + (x2.B - x1.B) * (x2.B - x1.B);

The GitHub Page for this repo has a demo that actually has a speedier version running (see code sample at bottom for notes):

http://zschuessler.github.io/DeltaE/demos/de76-chroma-key/