Created
February 20, 2016 21:18
-
-
Save msh9/16e43bbb28d8237c648c to your computer and use it in GitHub Desktop.
Testing different counter addition strategies
This file contains hidden or 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
'use strict'; | |
var RUNS = 10; | |
var ITERATIONS = 10000000; | |
function Counter(initialValue) { | |
this.val = initialValue; | |
} | |
Counter.prototype.addWithOr = function addWithOr(incrVal) { | |
this.val += incrVal || 1; | |
} | |
Counter.prototype.addWithIf = function addWithIf(incrVal) { | |
if(incrVal) { | |
this.val += incrVal; | |
} else { | |
this.val += 1; | |
} | |
} | |
Counter.prototype.addOne = function addOne() { | |
this.val += 1; | |
} | |
Counter.prototype.postFixIncrement = function postFixIncrement() { | |
this.val++; | |
} | |
function executeTests(runs, iterations) { | |
var startTime, diffTime, i, j, underTest, totalTime = [0,0]; | |
/**/ | |
console.log('Object counter using addWithOr'); | |
for (i = 0; i < runs; i++) { | |
underTest = new Counter(0); | |
startTime = process.hrtime(); | |
for (j = 0; j < iterations; j++) { | |
underTest.addWithOr(); | |
} | |
diffTime = process.hrtime(startTime); | |
totalTime[0] += diffTime[0]; | |
totalTime[1] += diffTime[1]; | |
} | |
console.log('Took %d seconds on average for %d iterations', (totalTime[0] + totalTime[1] / 1e9) / runs, iterations); | |
/**/ | |
totalTime = [0,0] | |
console.log('Object counter using addWithIf'); | |
for (i = 0; i < runs; i++) { | |
underTest = new Counter(0); | |
startTime = process.hrtime(); | |
for (j = 0; j < iterations; j++) { | |
underTest.addWithIf(); | |
} | |
diffTime = process.hrtime(startTime); | |
totalTime[0] += diffTime[0]; | |
totalTime[1] += diffTime[1]; | |
} | |
console.log('Took %d seconds on average for %d iterations', (totalTime[0] + totalTime[1] / 1e9) / runs, iterations); | |
/**/ | |
totalTime = [0,0] | |
console.log('Object counter using addOne'); | |
for (i = 0; i < runs; i++) { | |
underTest = new Counter(0); | |
startTime = process.hrtime(); | |
for (j = 0; j < iterations; j++) { | |
underTest.addOne(); | |
} | |
diffTime = process.hrtime(startTime); | |
totalTime[0] += diffTime[0]; | |
totalTime[1] += diffTime[1]; | |
} | |
console.log('Took %d seconds on average for %d iterations', (totalTime[0] + totalTime[1] / 1e9) / runs, iterations); | |
totalTime = [0,0] | |
console.log('Object counter using postFixIncrement'); | |
for (i = 0; i < runs; i++) { | |
underTest = new Counter(0); | |
startTime = process.hrtime(); | |
for (j = 0; j < iterations; j++) { | |
underTest.postFixIncrement(); | |
} | |
diffTime = process.hrtime(startTime); | |
totalTime[0] += diffTime[0]; | |
totalTime[1] += diffTime[1]; | |
} | |
console.log('Took %d seconds on average for %d iterations', (totalTime[0] + totalTime[1] / 1e9) / runs, iterations); | |
totalTime = [0,0] | |
console.log('Number object counter using postfix increment'); | |
for (i = 0; i < runs; i++) { | |
underTest = 0; | |
startTime = process.hrtime(); | |
for (j = 0; j < iterations; j++) { | |
underTest++; | |
} | |
diffTime = process.hrtime(startTime); | |
totalTime[0] += diffTime[0]; | |
totalTime[1] += diffTime[1]; | |
} | |
console.log('Took %d seconds on average for %d iterations', (totalTime[0] + totalTime[1] / 1e9) / runs, iterations); | |
totalTime = [0,0] | |
console.log('Number object counter using += 1 increment'); | |
for (i = 0; i < runs; i++) { | |
underTest = 0; | |
startTime = process.hrtime(); | |
for (j = 0; j < iterations; j++) { | |
underTest += 1; | |
} | |
diffTime = process.hrtime(startTime); | |
totalTime[0] += diffTime[0]; | |
totalTime[1] += diffTime[1]; | |
} | |
console.log('Took %d seconds on average for %d iterations', (totalTime[0] + totalTime[1] / 1e9) / runs, iterations); | |
/* */ | |
} | |
executeTests(RUNS, ITERATIONS); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following are several runs from my desktop: