Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Sylvester.Matrix.prototype = {
if (!this.isSquare) { return null; }
var els = [], n = this.elements.length;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zbjornson You know the length already in a lot these cases, you should use new Array(n) and then set the indexs x[i] = <val>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@megawac that actually seems detrimental in some browsers and only a minor improvement in others: http://jsperf.com/construct-array-of-fixed-size/2

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Specifically, I optimized for FF and chrome, and the gain in FF doesn't outweigh the loss in chrome.)

for (var i = 0; i < n; i++) {
els.push(this.elements[i][i]);
els[i] = this.elements[i][i];
}
return Sylvester.Vector.create(els);
},
Expand Down
37 changes: 22 additions & 15 deletions src/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ var $V = Sylvester.Vector.create;

Sylvester.Vector.Random = function(n) {
var elements = [];
while (n--) { elements.push(Math.random()); }
while (n--) { elements[n] = Math.random(); }
return Sylvester.Vector.create(elements);
};

Sylvester.Vector.Zero = function(n) {
var elements = [];
while (n--) { elements.push(0); }
while (n--) { elements[n] = 0; }
return Sylvester.Vector.create(elements);
};

Expand Down Expand Up @@ -48,7 +48,7 @@ Sylvester.Vector.prototype = {
map: function(fn, context) {
var elements = [];
this.each(function(x, i) {
elements.push(fn.call(context, x, i));
elements[i] = fn.call(context, x, i);
});
return Sylvester.Vector.create(elements);
},
Expand Down Expand Up @@ -107,13 +107,18 @@ Sylvester.Vector.prototype = {
},

subtract: function(vector) {
var V = vector.elements || vector;
if (this.elements.length !== V.length) { return null; }
return this.map(function(x, i) { return x - V[i-1]; });
var V = vector.elements || vector,
m = this.elements.length;
if (m !== V.length) { return null; }
var te = this.elements, elements = [];
while (m--) { elements[m] = te[m] - V[m]; }
return Sylvester.Vector.create(elements);
},

multiply: function(k) {
return this.map(function(x) { return x*k; });
var te = this.elements, m = te.length, elements = [];
while (m--) { elements[m] = te[m] * k; }
return Sylvester.Vector.create(elements);
},

dot: function(vector) {
Expand All @@ -136,17 +141,17 @@ Sylvester.Vector.prototype = {
},

max: function() {
var m = 0, i = this.elements.length;
var m = 0, te = this.elements, i = te.length;
while (i--) {
if (Math.abs(this.elements[i]) > Math.abs(m)) { m = this.elements[i]; }
if (Math.abs(te[i]) > Math.abs(m)) { m = te[i]; }
}
return m;
},

indexOf: function(x) {
var index = null, n = this.elements.length;
var index = null, te = this.elements, n = te.length;
for (var i = 0; i < n; i++) {
if (index === null && this.elements[i] === x) {
if (index === null && te[i] === x) {
index = i + 1;
}
}
Expand All @@ -158,13 +163,15 @@ Sylvester.Vector.prototype = {
},

round: function() {
return this.map(function(x) { return Math.round(x); });
var te = this.elements, m = te.length, elements = [];
while (m--) { elements[m] = Math.round(te[m]); }
return Sylvester.Vector.create(elements);
},

snapTo: function(x) {
return this.map(function(y) {
return (Math.abs(y - x) <= Sylvester.precision) ? x : y;
});
var te = this.elements, m = te.length, elements = [];
while (m--) { elements[m] = (Math.abs(te[m] - x) <= Sylvester.precision) ? x : te[m]; }
return Sylvester.Vector.create(elements);
},

distanceFrom: function(obj) {
Expand Down