Created
October 6, 2016 18:00
-
-
Save AlexTiTanium/b1f866585ccf242509aa88768bb621bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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