Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
if-else vs switch vs object 2
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser:
Chrome 128
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Benchmark engine:
Benchmark.js
if-else
6.5M/s
switch
6.5M/s
object
2.2M/s
object - rerun
2.1M/s
View exact numbers
Test name
Executions per second
✓
if-else
6,515,722 Ops/sec
switch
6,495,347 Ops/sec
object
2,152,254 Ops/sec
object - rerun
2,133,375 Ops/sec
Script Preparation code:
class Host { A = 1; B = 2; C = 3; } var host = new Host(); var x = Math.floor(Math.random() * 3); function testObject(h, p) { const obj = { [h.A]: 1, [h.B]: 2, [h.C]: 3, } return obj[p]; } function testIfElse(h, p) { var v = 0; if (p === h.A) { v = 1; } else if (p === h.B) { v = 2; } else if (p === h.C) { v = 3; } return v; } function testSwitch(h, p) { let v = 0; switch (p) { case h.A: v = 1; break; case h.B: v = 2; break; case h.B: v = 3; break; } return v; }
Tests:
object
testObject(host, x);
if-else
testIfElse(host, x);
switch
testSwitch(host, x)
object - rerun
testObject(host, x)