Skip to content

Instantly share code, notes, and snippets.

@msh9
Last active May 5, 2017 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msh9/cd26957480635f7aa076 to your computer and use it in GitHub Desktop.
Save msh9/cd26957480635f7aa076 to your computer and use it in GitHub Desktop.
Test different types of JavaScript looks for NodeJS v0.12.0
'use strict';
var iterations = 1000000,
outerIterations = 100;
function runit() {
var ary
,result
,idx;
ary = createData();
console.log('Executing while loop with prefix incre in test condition');
result = executeOuterLoop(whileLoopPrefixIncr, ary);
printResult(result);
console.log('Executing basic for loop');
result = executeOuterLoop(forLoop, ary);
printResult(result);
console.log('Executing for loop with precomputed length');
result = executeOuterLoop(forLoopPre, ary);
printResult(result);
console.log('Executing for loop with precomputed length and decr to zero');
result = executeOuterLoop(forLoopPreDecr, ary);
printResult(result);
console.log('Executing while loop with precomputed length');
result = executeOuterLoop(whileLoop, ary);
printResult(result);
console.log('Executing while loop with decr in test condition');
result = executeOuterLoop(whileLoopDecr, ary);
printResult(result);
console.log('Executing iterating with #forEach function');
result = executeOuterLoop(forEachFunc, ary);
printResult(result);
console.log('Executing iterating and creating new array with changes using map function');
result = executeOuterLoop(mapFunc, ary);
printResult(result);
}
function executeOuterLoop(loopFunc, ary) {
var idx,
sum = [0, 0],
result;
for (idx = 0; idx < outerIterations; idx++) {
result = loopFunc(ary.slice(0));
sum[0] += result[0];
sum[1] += result[1];
}
sum[0] = sum[0] / outerIterations;
sum[1] = sum[1] / outerIterations;
return sum;
}
function printResult(diff) {
console.log('Loop time was %d seconds and %d milliseconds', diff[0], diff[1] / 1000000);
}
function createData() {
var idx
,retVal = [];
for (idx = 0 ; idx < iterations; idx++) {
retVal.push(Math.floor((Math.random() * 10) + 1));
}
return retVal;
}
function forLoop(ary) {
var start
,diff
,idx;
start = process.hrtime();
for (idx = 0; idx < ary.length; idx++) {
ary[idx] += 1;
}
diff = process.hrtime(start);
return diff;
}
function forLoopPre(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
length = ary.length;
for (idx = 0; idx < length; idx++) {
ary[idx] += 1;
}
diff = process.hrtime(start);
return diff;
}
function forLoopPreDecr(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
length = ary.length;
for (idx = length - 1; idx >= 0; idx--) {
ary[idx] += 1;
}
diff = process.hrtime(start);
return diff;
}
function whileLoop(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
length = ary.length;
idx = 0;
while (idx < length) {
ary[idx] += 1;
idx++;
}
diff = process.hrtime(start);
return diff;
}
function whileLoopDecr(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
idx = ary.length;
while (idx--) {
ary[idx] += 1;
}
diff = process.hrtime(start);
return diff;
}
function whileLoopPrefixIncr(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
idx = -1;
length = ary.length;
while (++idx < length) {
ary[idx] += 1;
}
diff = process.hrtime(start);
return diff;
}
function forEachFunc(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
idx = -1;
ary.forEach(function (val, curIdx, curAry) {
curAry[curIdx] += 1;
});
diff = process.hrtime(start);
return diff;
}
function mapFunc(ary) {
var start
,diff
,idx
,length;
start = process.hrtime();
idx = -1;
ary.map(function (val) {
return val += 1;
});
diff = process.hrtime(start);
return diff;
}
runit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment