From e8162497fd9c4955186a54d1573c98cb321f1bb2 Mon Sep 17 00:00:00 2001 From: Alexey Berezin Date: Tue, 4 Sep 2018 00:51:15 +0300 Subject: [PATCH 1/3] refactror: add some improvements * replace unused variable with `_` * `throw` stops running the code in a function, so don't need to use `else` then * `||`-operator is working wrong when falsy values go, so better replace them with `!=`-operator (as it's not strict and only `undefined` and `null` are usable there) * better put conditions out of `for` loop to not evaluating the condition over and over for no need * `+0.0` and `0.0` are exactly same values * `[].slice` is shorter than `Array.prototype.slice` * sometimes saving values to an arguments can take more time, it might be ambiguous * `Math.max` and `Math.min` work longer than your own implementation * `check` function can take `any` values now with `Number.isFinite` * --- vector.js | 77 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/vector.js b/vector.js index 2bc5445b..fe4cd5d7 100644 --- a/vector.js +++ b/vector.js @@ -124,7 +124,7 @@ * @returns {Vector} this **/ Vector.prototype.scale = function (scalar) { - return this.each(function(value, i, data){ + return this.each(function(_, i, data) { data[i] *= scalar; }); }; @@ -183,17 +183,24 @@ Vector.fill = function (count, value, type) { if (count < 0) throw new Error('invalid size'); - else if (count === 0) + if (count === 0) return new Vector(); - - value = value || +0.0; - type = type || Float64Array; + + if (value == null) { + value = 0.0; + } + if (type == null) { + type = Float64Array; + } var data = new type(count), - isValueFn = typeof value === 'function', i; - for (i = 0; i < count; i++) - data[i] = isValueFn ? value(i) : value; + if (typeof value === 'function') + for (i = 0; i < count; i++) + data[i] = value(i); + else + for (i = 0; i < count; i++) + data[i] = value; return new Vector(data); }; @@ -207,7 +214,7 @@ * @returns {Vector} a new vector of the specified size and `type` **/ Vector.zeros = function (count, type) { - return Vector.fill(count, +0.0, type); + return Vector.fill(count, 0.0, type); }; /** @@ -234,8 +241,12 @@ * @returns {Vector} a new vector of the specified size and `type` **/ Vector.random = function (count, deviation, mean, type) { - deviation = deviation || 1; - mean = mean || 0; + if (deviation == null) { + deviation = 1; + } + if (mean == null) { + mean = 0; + } return Vector.fill(count, function() { return deviation * Math.random() + mean; }, type); @@ -254,7 +265,7 @@ * @returns {Vector} a new vector containing the specified range of the specified `type` **/ Vector.range = function () { - var args = [].slice.call(arguments, 0), + var args = [].slice.call(arguments), backwards = false, start, step, end; @@ -262,7 +273,7 @@ if (typeof args[args.length - 1] === 'function') type = args.pop(); - switch(args.length) { + switch (args.length) { case 2: end = args.pop(); step = 1; @@ -288,9 +299,14 @@ throw new Error('invalid range'); var data = new type(Math.ceil((end - start) / step)), - i, j; - for (i = start, j = 0; i < end; i += step, j++) - data[j] = backwards ? end - i + start : i; + i = start, + j = 0; + if (backwards) + for (; i < end; i += step, j++) + data[j] = end - i + start; + else + for (; i < end; i += step, j++) + data[j] = i; return new Vector(data); }; @@ -387,14 +403,10 @@ if (this.length !== vector.length) return false; - var a = this.data, - b = vector.data, - length = this.length, - i = 0; - - while (i < length && a[i] === b[i]) + var i = 0; + while (i < this.length && this.data[i] === vector.data[i]) i++; - return i === length; + return i === this.length; }; /** @@ -404,7 +416,7 @@ **/ Vector.prototype.min = function () { return this.reduce(function(acc, item) { - return Math.min(acc, item); + return acc < item ? acc : item; }, Number.POSITIVE_INFINITY); }; @@ -415,7 +427,7 @@ **/ Vector.prototype.max = function () { return this.reduce(function(acc, item) { - return Math.max(acc, item); + return acc < item ? item : acc; }, Number.NEGATIVE_INFINITY); }; @@ -425,7 +437,7 @@ * @param {Number} index **/ Vector.prototype.check = function (index) { - if (Number.isNaN(index) || index < 0 || index > this.length - 1) + if (!Number.isFinite(index) || index < 0 || index > this.length - 1) throw new Error('index out of bounds'); } @@ -591,7 +603,7 @@ throw new Error('Reduce of empty matrix with no initial value.'); var i = 0, - value = initialValue || this.data[i++]; + value = initialValue != null ? initialValue : this.data[i++]; for (; i < l; i++) value = callback.call(this, value, this.data[i], i, this.data); @@ -605,9 +617,12 @@ **/ Vector.prototype.toString = function () { var result = ['['], - i; - for (i = 0; i < this.length; i++) - result.push(i > 0 ? ', ' + this.data[i] : this.data[i]); + i = 0; + + if (i < this.length) + result.push(this.data[i++]); + while (i < this.length) + result.push(', ' + this.data[i++]); result.push(']'); @@ -623,7 +638,7 @@ if (!this.data) return []; - return Array.prototype.slice.call(this.data); + return [].slice.call(this.data); }; module.exports = Vector; From f25b0a607d542e8bda48eb4dbe7a7275a221d7c7 Mon Sep 17 00:00:00 2001 From: Mateo Gianolio Date: Tue, 4 Sep 2018 23:31:09 +0200 Subject: [PATCH 2/3] update travis node version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0a7d4731..9b414df8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - "4.0" + - "6.9.0" before_install: - sudo apt-get install libblas-dev env: From 76b8e2c9dea59c7841a74e44aee31ceb0cf3aa0c Mon Sep 17 00:00:00 2001 From: Mateo Gianolio Date: Tue, 4 Sep 2018 23:38:17 +0200 Subject: [PATCH 3/3] bump version and generate new dist --- dist/vectorious.min.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/vectorious.min.js b/dist/vectorious.min.js index 674b2fe1..ddcc71ef 100644 --- a/dist/vectorious.min.js +++ b/dist/vectorious.min.js @@ -1 +1 @@ -!function i(a,s,h){function u(r,t){if(!s[r]){if(!a[r]){var e="function"==typeof require&&require;if(!t&&e)return e(r,!0);if(p)return p(r,!0);var n=new Error("Cannot find module '"+r+"'");throw n.code="MODULE_NOT_FOUND",n}var o=s[r]={exports:{}};a[r][0].call(o.exports,function(t){return u(a[r][1][t]||t)},o,o.exports,i,a,s,h)}return s[r].exports}for(var p="function"==typeof require&&require,t=0;tthis.shape[0]-1||r>this.shape[1]-1)throw new Error("index out of bounds")},c.prototype.get=function(t,r){return this.check(t,r),this.data[t*this.shape[1]+r]},c.prototype.set=function(t,r,e){return this.check(t,r),this.data[t*this.shape[1]+r]=e,this},c.prototype.swap=function(t,r){if(t<0||r<0||t>this.shape[0]-1||r>this.shape[0]-1)throw new Error("index out of bounds");var e=this.shape[1],n=this.data.slice(t*e,(t+1)*e);return this.data.copyWithin(t*e,r*e,(r+1)*e),this.data.set(n,r*e),this},c.prototype.map=function(t){var r,e=this.shape[0],n=this.shape[1],o=e*n,i=new c(this),a=i.data;for(r=0;rthis.length-1)throw new Error("index out of bounds")},p.prototype.get=function(t){return this.check(t),this.data[t]},p.prototype.set=function(t,r){return this.check(t),this.data[t]=r,this},Object.defineProperties(p.prototype,{x:t(0),y:t(1),z:t(2),w:t(3)}),p.combine=function(t,r){return new p(t).combine(r)},p.prototype.combine=function(t){if(!t.length)return this;if(!this.length)return this.data=new t.type(t.data),this.length=t.length,this.type=t.type,this;var r=this.length,e=t.length,n=this.data,o=t.data,i=new this.type(r+e);return i.set(n),i.set(o,r),this.data=i,this.length=r+e,this},p.prototype.push=function(t){return this.combine(new p([t]))},p.prototype.map=function(t){var r,e=new p(this),n=e.data;for(r=0;rthis.shape[0]-1||r>this.shape[1]-1)throw new Error("index out of bounds")},c.prototype.get=function(t,r){return this.check(t,r),this.data[t*this.shape[1]+r]},c.prototype.set=function(t,r,e){return this.check(t,r),this.data[t*this.shape[1]+r]=e,this},c.prototype.swap=function(t,r){if(t<0||r<0||t>this.shape[0]-1||r>this.shape[0]-1)throw new Error("index out of bounds");var e=this.shape[1],n=this.data.slice(t*e,(t+1)*e);return this.data.copyWithin(t*e,r*e,(r+1)*e),this.data.set(n,r*e),this},c.prototype.map=function(t){var r,e=this.shape[0],n=this.shape[1],o=e*n,i=new c(this),a=i.data;for(r=0;rthis.length-1)throw new Error("index out of bounds")},p.prototype.get=function(t){return this.check(t),this.data[t]},p.prototype.set=function(t,r){return this.check(t),this.data[t]=r,this},Object.defineProperties(p.prototype,{x:t(0),y:t(1),z:t(2),w:t(3)}),p.combine=function(t,r){return new p(t).combine(r)},p.prototype.combine=function(t){if(!t.length)return this;if(!this.length)return this.data=new t.type(t.data),this.length=t.length,this.type=t.type,this;var r=this.length,e=t.length,n=this.data,o=t.data,i=new this.type(r+e);return i.set(n),i.set(o,r),this.data=i,this.length=r+e,this},p.prototype.push=function(t){return this.combine(new p([t]))},p.prototype.map=function(t){var r,e=new p(this),n=e.data;for(r=0;r