Created
February 7, 2012 19:53
-
-
Save togakangaroo/1761525 to your computer and use it in GitHub Desktop.
Javascript hashmap vs. if vs. case
This file contains 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
//console.time implementation for IE | |
if (window.console && typeof (window.console.time) == "undefined") { | |
console.time = function (name, reset) { | |
if (!name) { | |
return; | |
} | |
var time = new Date().getTime(); | |
if (!console.timeCounters) { | |
console.timeCounters = {} | |
}; | |
var key = "KEY" + name.toString(); | |
if (!reset && console.timeCounters[key]) { | |
return; | |
} | |
console.timeCounters[key] = time; | |
}; | |
console.timeEnd = function (name) { | |
var time = new Date().getTime(); | |
if (!console.timeCounters) { | |
return; | |
} | |
var key = "KEY" + name.toString(); | |
var timeCounter = console.timeCounters[key]; | |
if (timeCounter) { | |
var diff = time - timeCounter; | |
var label = name + ": " + diff + "ms"; | |
console.info(label); | |
delete console.timeCounters[key]; | |
} | |
return diff; | |
}; | |
} | |
var withIf = function (conditionCount, stringKeys, coerce) { | |
var key = function (i) { | |
return stringKeys ? "'" + i + "'" : i | |
}, | |
equals = coerce ? "===" : "==", | |
funcParts = ["function(x) {"]; | |
if (conditionCount > 0) funcParts.push("if(x " + equals + " 0) { return true; } "); | |
for (var i = 1; i < conditionCount; i++) { | |
funcParts.push("else if(x " + equals + " " + key(i) + ") { return true; }"); | |
} | |
if (conditionCount > 0) funcParts.push("else { return false; } "); | |
funcParts.push("}") | |
return Function("return " + funcParts.join("\n"))(); | |
}, | |
withHash = function (conditionCount, stringKeys) { | |
var key = function (i) { | |
return stringKeys ? "'" + i + "'" : i | |
}, | |
f = ["return (function() {"], | |
f2 = []; | |
f.push(" var hashMap = {"); | |
for (var i = 0; i < conditionCount; i++) { | |
f2.push(key(i) + ": function() { return true; }"); | |
} | |
f.push(f2.join(",\n")); | |
f.push(" };"); | |
f.push(" return function(x) {"); | |
f.push(" return hashMap[x] ? hashMap[x]() : false;"); | |
f.push(" }"); | |
f.push("})()"); | |
return Function(f.join("\n"))(); | |
}, | |
withCase = function (conditionCount, stringKeys) { | |
var key = function (i) { | |
return stringKeys ? "'" + i + "'" : i | |
}, | |
funcParts = ["function(x) {", " switch(x) {"]; | |
for (var i = 0; i < conditionCount; i++) { | |
funcParts.push(" case " + key(i) + ": return true;break;"); | |
} | |
funcParts.push(" default: return false;"); | |
funcParts.push(" }") | |
funcParts.push("}") | |
return Function("return " + funcParts.join("\n"))(); | |
}; | |
var conditions = 100, | |
test = null, | |
testTimes = 15000; | |
test = withIf(conditions, false, false); | |
console.time('with if int keys no coerce'); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i); | |
} | |
} | |
console.timeEnd('with if int keys no coerce'); | |
test = withIf(conditions, false, true); | |
console.time('with if int keys with coerce'); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i); | |
} | |
} | |
console.timeEnd('with if int keys with coerce'); | |
test = withIf(conditions, true, false); | |
console.time('with if string keys no coerce'); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i.toString()); | |
} | |
} | |
console.timeEnd('with if string keys no coerce'); | |
test = withIf(conditions, true, true); | |
console.time('with if string keys with coerce'); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i.toString()); | |
} | |
} | |
console.timeEnd('with if string keys with coerce'); | |
console.time('with hash int keys'); | |
test = withHash(conditions, false); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i); | |
} | |
} | |
console.timeEnd('with hash int keys'); | |
console.time('with hash string keys'); | |
test = withHash(conditions, false); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i.toString()); | |
} | |
} | |
console.timeEnd('with hash string keys'); | |
console.time('with case int keys'); | |
test = withCase(conditions, false); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i); | |
} | |
} | |
console.timeEnd('with case int keys'); | |
console.time('with case string keys'); | |
test = withCase(conditions, false); | |
for (var times = 0; times < testTimes; times++) { | |
for (var i = 0; i < conditions; i++) { | |
test(i.toString()); | |
} | |
} | |
console.timeEnd('with case string keys'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment