diff --git a/benchmarks/vector.js b/benchmarks/vector.js index d5871bfc..a083f269 100644 --- a/benchmarks/vector.js +++ b/benchmarks/vector.js @@ -31,6 +31,7 @@ .add('a.magnitude()', function () { a.magnitude(); }) .add('a.angle(b)', function () { a.angle(b); }) .add('a.project(b)', function () { a.project(b); }) + .add('Vector.random(' + N + ').min()', function () { Vector.random(N).min(); }) .on('cycle', function (event) { console.log(String(event.target)); }) .run(); }()); diff --git a/vector.js b/vector.js index e1b9e237..c317964f 100644 --- a/vector.js +++ b/vector.js @@ -567,9 +567,18 @@ * @returns {Number} the smallest element of the current vector **/ Vector.prototype.min = function () { - return this.reduce(function(acc, item) { - return Math.min(acc, item); - }, Number.POSITIVE_INFINITY); + var min = Number.POSITIVE_INFINITY, + data = this.data, + value, + i, l; + + for (i = 0, l = data.length; i < l; i++) { + value = data[i]; + if (value < min) + min = value; + } + + return min; }; /** @@ -577,9 +586,18 @@ * @returns {Number} the largest element of current vector **/ Vector.prototype.max = function () { - return this.reduce(function(acc, item) { - return Math.max(acc, item); - }, Number.NEGATIVE_INFINITY); + var max = Number.NEGATIVE_INFINITY, + data = this.data, + value, + i, l; + + for (i = 0, l = this.length; i < l; i++) { + value = data[i]; + if (value > max) + max = value; + } + + return max; }; /**