Skip to content

Instantly share code, notes, and snippets.

@Me1000
Created December 30, 2012 21:34
Show Gist options
  • Save Me1000/4415362 to your computer and use it in GitHub Desktop.
Save Me1000/4415362 to your computer and use it in GitHub Desktop.
Array.prototype.myJoin = function(aDelemiter)
{
if (aDelemiter === null)
aDelemiter = ",";
if (this.length > 0)
var str = this[0];
for (var i = 1; i < this.length; i++) {
str += aDelemiter + this[i];
}
return str;
}
function runTest(objcCount, aDelemiter)
{
var manualTotal = 0;
var autoTotal = 0;
var builtInTime = 0;
var manualTime = 0;
var objs = [];
//console.log("populating array");
for (var i = 0; i < objcCount; i++) {
objs[i] = String(i);
};
for (var i = 0; i < 4; i++)
{
var start = new Date();
var out = objs.join(aDelemiter);
var end = new Date();
builtInTime = end - start;
var start2 = new Date();
var out = objs.myJoin("");
var end2 = new Date();
manualTime = end2 - start2;
console.log("Test " + i + ": ");
console.log(" Manual Time: " + manualTime / 1000 + "s");
console.log(" Built In Time: " + builtInTime / 1000 + "s");
manualTotal += manualTime;
autoTotal += builtInTime;
}
console.log("Average of " + objcCount + " objects:");
console.log("Manual Time: " + manualTotal / 4000 + "s");
console.log("Built In Time: " + autoTotal / 4000 + "s");
if (manualTotal < autoTotal)
console.log("Manual join is faster");
else if (autoTotal < manualTotal)
console.log("Built in join is faster");
else
console.log("Built in and manual joins are the same");
console.log("----------------------------------------");
console.log("----------------------------------------");
}
runTest( 10000, "");
runTest( 250000, "");
runTest( 500000, "");
runTest(1000000, "");
runTest(2000000, "");
runTest(3000000, "");
runTest(4000000, "");
Test 0:
Manual Time: 0.001s
Built In Time: 0s
Test 1:
Manual Time: 0s
Built In Time: 0s
Test 2:
Manual Time: 0.003s
Built In Time: 0s
Test 3:
Manual Time: 0s
Built In Time: 0s
Average of 10000 objects:
Manual Time: 0.001s
Built In Time: 0s
Built in join is faster
----------------------------------------
----------------------------------------
Test 0:
Manual Time: 0.033s
Built In Time: 0.066s
Test 1:
Manual Time: 0.008s
Built In Time: 0.079s
Test 2:
Manual Time: 0.029s
Built In Time: 0.044s
Test 3:
Manual Time: 0.007s
Built In Time: 0.066s
Average of 250000 objects:
Manual Time: 0.01925s
Built In Time: 0.06375s
Manual join is faster
----------------------------------------
----------------------------------------
Test 0:
Manual Time: 0.057s
Built In Time: 0.093s
Test 1:
Manual Time: 0.09s
Built In Time: 0.07s
Test 2:
Manual Time: 0.061s
Built In Time: 0.061s
Test 3:
Manual Time: 0.101s
Built In Time: 0.064s
Average of 500000 objects:
Manual Time: 0.07725s
Built In Time: 0.072s
Built in join is faster
----------------------------------------
----------------------------------------
Test 0:
Manual Time: 0.134s
Built In Time: 0.242s
Test 1:
Manual Time: 0.218s
Built In Time: 0.2s
Test 2:
Manual Time: 0.174s
Built In Time: 0.199s
Test 3:
Manual Time: 0.209s
Built In Time: 0.233s
Average of 1000000 objects:
Manual Time: 0.18375s
Built In Time: 0.2185s
Manual join is faster
----------------------------------------
----------------------------------------
Test 0:
Manual Time: 0.327s
Built In Time: 0.308s
Test 1:
Manual Time: 0.393s
Built In Time: 0.277s
Test 2:
Manual Time: 0.307s
Built In Time: 0.283s
Test 3:
Manual Time: 0.412s
Built In Time: 0.284s
Average of 2000000 objects:
Manual Time: 0.35975s
Built In Time: 0.288s
Built in join is faster
----------------------------------------
----------------------------------------
Test 0:
Manual Time: 0.602s
Built In Time: 0.355s
Test 1:
Manual Time: 0.497s
Built In Time: 0.363s
Test 2:
Manual Time: 0.464s
Built In Time: 0.365s
Test 3:
Manual Time: 0.66s
Built In Time: 0.359s
Average of 3000000 objects:
Manual Time: 0.55575s
Built In Time: 0.3605s
Built in join is faster
----------------------------------------
----------------------------------------
Test 0:
Manual Time: 0.799s
Built In Time: 0.865s
Test 1:
Manual Time: 0.61s
Built In Time: 0.928s
Test 2:
Manual Time: 0.864s
Built In Time: 0.86s
Test 3:
Manual Time: 0.645s
Built In Time: 0.886s
Average of 4000000 objects:
Manual Time: 0.7295s
Built In Time: 0.88475s
Manual join is faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment