Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Switch vs Functional Approach 3
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
10824798.0 Ops/sec
Switch Fn style
6031819.0 Ops/sec
Script Preparation code:
const use = (a, fn) => fn(a); const isFunc = (v) => typeof v === 'function'; const isDefined = v => v !== undefined && v !== null; const switchEnum = (e, handlers) => use(handlers[e] || handlers.else, fn => isFunc(fn) ? fn(e) : e); function normal(inp) { switch (inp) { case "today": return 1; case "tomorrow": return 2; case "yesterday": return -1; default: return 0; } }; function funcccy(inp) { return switchEnum(inp, { "today": () => 1, "tomorrow": () => 2, "yesterday": () => -1, "else": () => 0, }); }
Tests:
Vanilla Switch
normal('today'); normal('yesterday'); normal('tomorrow');
Switch Fn style
funcccy('today'); funcccy('yesterday'); funcccy('tomorrow');