Skip to content

Instantly share code, notes, and snippets.

@AlexTiTanium
Created October 6, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexTiTanium/b1f866585ccf242509aa88768bb621bd to your computer and use it in GitHub Desktop.
Save AlexTiTanium/b1f866585ccf242509aa88768bb621bd to your computer and use it in GitHub Desktop.
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.getSum = function() {
return this.x + this.y;
};
function functionOne(a, b, c, d) {
return {
x: a.x + b.x + c.x + d.x,
y: a.y + b.y + c.y + d.y
};
};
function functionTwo(ax, bx, cx, dx, ay, by, cy, dy, resultObject) {
resultObject.x = ax + bx + cx +dx;
resultObject.y = ay + by + cy +dy;
return resultObject;
};
function functionThree(ax, bx, cx, dx, ay, by, cy, dy) {
return new Point(ax + bx + cx + dx, ay + by + cy + dy);
};
var iterations = 10000000;
console.time('Function Object creation #1');
for(var i = 0; i < iterations; i++ ){
var a = { x: Math.random(), y: Math.random() };
var b = { x: Math.random(), y: Math.random() };
var c = { x: Math.random(), y: Math.random() };
var d = { x: Math.random(), y: Math.random() };
var result = functionOne(a, b, c, d);
};
console.timeEnd('Function Object creation #1')
console.time('Function re-used obejct result #2');
var resultObject = { x: null, y: null };
for(var i = 0; i < iterations; i++ ){
var ax = Math.random();
var bx = Math.random();
var cx = Math.random();
var dx = Math.random();
var ay = Math.random();
var by = Math.random();
var cy = Math.random();
var dy = Math.random();
var result = functionTwo(ax, bx, cx, dx, ay, by, cy, dy, resultObject);
};
console.timeEnd('Function re-used obejct result #2')
console.time('Function new Point creation #3');
for(var i = 0; i < iterations; i++ ){
var ax = Math.random();
var bx = Math.random();
var cx = Math.random();
var dx = Math.random();
var ay = Math.random();
var by = Math.random();
var cy = Math.random();
var dy = Math.random();
var result = functionThree(ax, bx, cx, dx, ay, by, cy, dy);
};
console.timeEnd('Function new Point creation #3');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment