Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs lookup
(version: 0)
Comparing performance of:
Switch vs Object lookup
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var pageNumber = 'test'; var pageRouteName; var lookup = { One: 'create-very-one', Two: 'create-very-two', Three: 'create-very-three', Four: 'create-very-four', Five: 'create-very-five', Six: 'create-very-six', Seven: 'create-very-seven', Eight: 'create-very-eight', Nine: 'create-very-nine' }
Tests:
Switch
switch (pageNumber) { case 'One': pageRouteName = 'create-very-one'; break; case 'Two': pageRouteName = 'create-very-two'; break; case 'Three': pageRouteName = 'create-very-three'; break; case 'Four': pageRouteName = 'create-very-four'; break; case 'Five': pageRouteName = 'create-very-five'; break; case 'Six': pageRouteName = 'create-very-six'; break; case 'Seven': pageRouteName = 'create-very-seven'; break; case 'Eight': pageRouteName = 'create-very-eight'; break; case 'Nine': pageRouteName = 'create-very-nine'; break; default: { throw new Error(`Page of given number is not allowed. Given page number: ${pageNumber}.`); } }
Object lookup
pageRouteName = lookup[pageNumber];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch
Object lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
0.0 Ops/sec
Object lookup
596026368.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of two methods for determining a string value based on a given input: a `switch` statement and an object lookup using a property name. Let's break down each option: **1. `Switch` Statement:** This is a traditional control flow structure in JavaScript that evaluates an expression (in this case, the `pageNumber`) against several possible cases. If a match is found, the corresponding code block is executed. * **Pros:** Can be very readable and concise for simple scenarios like this. * **Cons:** Performance can suffer if there are many cases, as it involves comparing the input to each case sequentially. **2. Object Lookup:** This method uses an object (`lookup`) where keys represent possible `pageNumber` values and values are the corresponding strings. The code directly accesses the value associated with the given `pageNumber`. * **Pros:** Generally faster than a `switch` statement, especially for a large number of cases, because it involves direct access to the desired property (which can be an efficient lookup in modern JavaScript engines). * **Cons:** Can be slightly less readable if there are many entries in the object. **Considerations:** * **Number of Cases:** The `switch` statement might perform better for a small number of cases, while the object lookup shines when dealing with many cases. * **Maintainability:** The object lookup approach can sometimes be more maintainable as adding or removing cases involves simply modifying the object. **Alternatives:** * **Maps (ES6):** Similar to objects but provide faster key lookups due to their internal implementation (hash tables). In this specific benchmark, the `Object lookup` method clearly outperforms the `switch` statement. This aligns with general expectations about performance differences between these approaches.
Related benchmarks:
Native Number vs Lodash toNumber
+string vs Number vs parseInt
parseInt vs Number vs +
parseInt vs Number, number from ID
typeof number vs. Number.isNan vs. isNan vs self comparison. Versus let
Comments
Confirm delete:
Do you really want to delete benchmark?