Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Switch vs Functional Approach
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser:
Firefox 125
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Vanilla Switch
63907816.0 Ops/sec
Switch Fn style
8259009.5 Ops/sec
Tests:
Vanilla Switch
function isFunc (v) { return typeof v === 'function'; } function isDefined (v) { return v !== undefined && v !== null; } function switchEnum(e, handlers) { const specific = handlers[e]; if (isDefined(specific)) { return isFunc(specific) ? specific(e) : specific; } if (isDefined(handlers.else)) { return isFunc(handlers.else) ? handlers.else(e) : handlers.else; } throw new Error(`Unhandled switchEnum statement for value (${e}).`); } function normal(inp) { switch (inp) { case "today": return 1; case "tomorrow": return 2; case "yesterday": return -1; default: return 0; } }; normal('today'); normal('yesterday'); normal('tomorrow');
Switch Fn style
function isFunc (v) { return typeof v === 'function'; } function isDefined (v) { return v !== undefined && v !== null; } function switchEnum(e, handlers) { const specific = handlers[e]; if (isDefined(specific)) { return isFunc(specific) ? specific(e) : specific; } if (isDefined(handlers.else)) { return isFunc(handlers.else) ? handlers.else(e) : handlers.else; } throw new Error(`Unhandled switchEnum statement for value (${e}).`); } function funcccy(inp) { return switchEnum(inp, { "today": () => 1, "tomorrow": () => 2, "yesterday": () => -1, "else": () => 0, }); } funcccy('today'); funcccy('yesterday'); funcccy('tomorrow');