diff --git a/src/ol/math.js b/src/ol/math.js index 22e5c0fca2..dfd6e47673 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -17,13 +17,30 @@ ol.math.clamp = function(value, min, max) { /** + * Return the hyperbolic cosine of a given number. The method will use the + * native `Math.cosh` function if it is available, otherwise the hyperbolic + * cosine will be calculated via the reference implementation of the Mozilla + * developer network. + * * @param {number} x X. * @return {number} Hyperbolic cosine of x. */ -ol.math.cosh = function(x) { - var y = Math.exp(x); - return (y + 1 / y) / 2; -}; +ol.math.cosh = (function() { + // Wrapped in a iife, to save the overhead of checking for the native + // implementation on every invocation. + var cosh; + if ('cosh' in Math) { + // The environment supports the native Math.cosh function, use it… + cosh = Math.cosh; + } else { + // … else, use the reference implementation of MDN: + cosh = function(x) { + var y = Math.exp(x); + return (y + 1 / y) / 2; + }; + } + return cosh; +}()); /**