Skip to content

Instantly share code, notes, and snippets.

@brendanmckenzie brendanmckenzie/test.js
Last active Jun 25, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.