Skip to content

Instantly share code, notes, and snippets.

@sharikovvladislav
Created May 24, 2016 21:06
Show Gist options
  • Save sharikovvladislav/94440028d67fbb2592f3787233257ae4 to your computer and use it in GitHub Desktop.
Save sharikovvladislav/94440028d67fbb2592f3787233257ae4 to your computer and use it in GitHub Desktop.
var testSize = 10000;
prepare();
runTest();
function prepare () {
window.arr = [];
for (var i = 0; i < 1000; i++) window.arr[i] = 0;
}
function functionA () {
for (var key in arr) arr[key]++;
}
function functionB () {
for (var i = 0; i < arr.length; i++) arr[i]++;
}
function getEvaluationTime(fooToEvaluate) {
var date = new Date();
fooToEvaluate();
return new Date() - date;
}
function runTest () {
var firstResults = [],
secondResults = [];
for (var i = 0; i < testSize; i++) {
firstResults.push(getEvaluationTime(function () {
functionA();
}));
secondResults.push(getEvaluationTime(function () {
functionB();
}));
}
var firstResult = firstResults.reduce(function (sum, current) {
return sum + current;
}, 0);
var secondResult = secondResults.reduce(function (sum, current) {
return sum + current;
}, 0);
console.log('functionA result:', firstResult);
console.log('functionB result:', secondResult);
console.log('difference: functionA time is greater then functionB time ', firstResult/secondResult);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment