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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser:
Chrome 122
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Vanilla Switch
176351536.0 Ops/sec
Switch Fn style
3866539.8 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');