Skip to content

Instantly share code, notes, and snippets.

@msh9
Created February 20, 2016 21:18
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 msh9/16e43bbb28d8237c648c to your computer and use it in GitHub Desktop.
Save msh9/16e43bbb28d8237c648c to your computer and use it in GitHub Desktop.
Testing different counter addition strategies
'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);
@msh9
Copy link
Author

msh9 commented Feb 20, 2016

The following are several runs from my desktop:

PS D:\UserData\michael\Desktop> node .\test-adding.js
Object counter using addWithOr
Took 0.026950022400000002 seconds on average for 10000000 iterations
Object counter using addWithIf
Took 0.022100082400000002 seconds on average for 10000000 iterations
Object counter using addOne
Took 0.019960927 seconds on average for 10000000 iterations
Object counter using postFixIncrement
Took 0.0198389931 seconds on average for 10000000 iterations
Number object counter using postfix increment
Took 0.013309491900000001 seconds on average for 10000000 iterations
Number object counter using += 1 increment
Took 0.0133281643 seconds on average for 10000000 iterations
PS D:\UserData\michael\Desktop> node .\test-adding.js
Object counter using postFixIncrement
Took 0.0210680646 seconds on average for 10000000 iterations
Number object counter using postfix increment
Took 0.013184181 seconds on average for 10000000 iterations
Number object counter using += 1 increment
Took 0.0130616183 seconds on average for 10000000 iterations
Object counter using addWithOr
Took 0.0381299929 seconds on average for 10000000 iterations
Object counter using addWithIf
Took 0.0196902755 seconds on average for 10000000 iterations
Object counter using addOne
Took 0.019644852600000002 seconds on average for 10000000 iterations
PS D:\UserData\michael\Desktop> node .\test-adding.js
Object counter using postFixIncrement
Took 0.0211101772 seconds on average for 10000000 iterations
Number object counter using postfix increment
Took 0.013163058799999999 seconds on average for 10000000 iterations
Number object counter using += 1 increment
Took 0.013055758300000001 seconds on average for 10000000 iterations
Object counter using addWithOr
Took 0.0377697204 seconds on average for 10000000 iterations
Object counter using addWithIf
Took 0.0198723652 seconds on average for 10000000 iterations
Object counter using addOne
Took 0.019933547500000003 seconds on average for 10000000 iterations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment