Last active
June 25, 2019 05:56
-
-
Save brendanmckenzie/0be92e747b3234ea228ed5c83d4a5fca to your computer and use it in GitHub Desktop.
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
const ifTask = () => { | |
const color = ""; | |
if (color == "red") { | |
//do something | |
} else if (color == "blue") { | |
//do something | |
} else if (color == "brown") { | |
//do something | |
} else if (color == "black") { | |
//do something | |
} else { | |
//do something | |
} | |
}; | |
const ifNoElseTask = () => { | |
const color = ""; | |
if (color == "red") { | |
//do something | |
return; | |
} | |
if (color == "blue") { | |
//do something | |
return; | |
} | |
if (color == "brown") { | |
//do something | |
return; | |
} | |
if (color == "black") { | |
//do something | |
return; | |
} | |
//do something | |
return; | |
}; | |
const switchTask = () => { | |
const color = ""; | |
switch (color) { | |
case "red": | |
//do something | |
break; | |
case "blue": | |
//do something | |
break; | |
case "brown": | |
//do something | |
break; | |
case "black": | |
//do something | |
break; | |
default: | |
//do something | |
} | |
}; | |
const lookupTask = () => { | |
const color = ""; | |
const def = () => { | |
/*do something*/ | |
}; | |
const dict = { | |
red: () => { | |
/*do something*/ | |
}, | |
blue: () => { | |
/*do something*/ | |
}, | |
brown: () => { | |
/*do something*/ | |
}, | |
black: () => { | |
/*do something*/ | |
} | |
}; | |
const task = dict[color] || def; | |
def(); | |
}; | |
const runTest = (name, task, iterations) => { | |
const start = new Date(); | |
for (var i = 0; i < iterations; i++) { | |
task(); | |
} | |
const end = new Date(); | |
const ticks = end.getTime() - start.getTime(); | |
console.log(`finished '${name}' - ${ticks}ms after ${iterations} iterations`); | |
}; | |
const iterations = 100000000; | |
runTest("if", ifTask, iterations); | |
runTest("ifNoElse", ifNoElseTask, iterations); | |
runTest("switch", switchTask, iterations); | |
runTest("lookup", lookupTask, iterations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment