Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs Object Literal w/out console.log 3
(version: 0)
Comparing performance of:
Switch vs Object Literal
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'abcdef'; str = str.charAt(Math.floor(Math.random() * 6)); function fn1() {return 'one'}; function fn2() {return 'two'}; function fn3() {return 'three'}; var objLiteral = { a: fn1, b: fn2, c: fn3, d: fn1, e: fn2, f: fn3 }; function switchFn(s) { switch (s) { case 'a': return fn1(); case 'b': return fn2(); case 'c': return fn3(); case 'd': return fn1(); case 'e': return fn2(); case 'f': return fn3(); } }
Tests:
Switch
switchFn(str);
Object Literal
objLiteral[str]();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch
Object Literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance difference between two approaches: using a `switch` statement and an object literal with bracket notation to access functions. **Switch vs Object Literal without console.log 3** The test case "Switch vs Object Literal w/out console.log 3" uses a simple string manipulation scenario: 1. A random character is selected from the string `'abcdef'`. 2. Three functions (`fn1`, `fn2`, and `fn3`) are defined, each returning a different string. 3. An object literal `objLiteral` is created with the three functions as its properties, where the property names match the first two characters of the selected random character. The benchmark tests the performance of two approaches: **Approach 1: Switch Statement** The `switchFn(s)` function uses a `switch` statement to access the corresponding function based on the value of the `s` variable. The `case` statements are evaluated in order until one matches, and the associated function is called. ```javascript function switchFn(s) { switch (s) { case 'a': return fn1(); case 'b': return fn2(); case 'c': return fn3(); case 'd': return fn1(); case 'e': return fn2(); case 'f': return fn3(); } } ``` **Approach 2: Object Literal with Bracket Notation** The object literal `objLiteral` is used to access the corresponding function based on the value of the `s` variable. The syntax `[objLiteral[str]][0]` is used, where `[0]` is the index of the first element in the array returned by `objLiteral[str]`. ```javascript var objLiteral = { a: fn1, b: fn2, c: fn3, d: fn1, e: fn2, f: fn3 }; // ... ``` **Pros and Cons of Each Approach** **Switch Statement:** Pros: * Can be more readable and maintainable for small sets of cases. * Does not require creating an object literal or indexing into it. Cons: * Can be slower due to the overhead of evaluating `case` statements. * May not perform as well for large sets of cases. **Object Literal with Bracket Notation:** Pros: * Can be more concise and expressive, especially for larger datasets. * Does not require using a `switch` statement or creating an object literal. Cons: * Requires creating an object literal, which can be slower to parse. * May not perform as well due to the indexing operation `[0]`. **Library: `Math.random()`** The `Math.random()` function is used to generate a random character from the string `'abcdef'`. This library provides a simple way to generate random numbers. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches** Other approaches that could be used for this benchmark include: * Using `if` statements instead of a `switch` statement. * Using an array with a `find` method to access the corresponding function. * Using a library like Lodash's `get` function to access the corresponding value. These alternatives may have different performance characteristics and trade-offs in terms of readability and maintainability.
Related benchmarks:
Switch vs Object Literal w/out console.log 2
Switch vs Object Literal w/out console.log 4
Switch vs Object Literal larger
Switch vs Object Literal (fixed prep code)
Comments
Confirm delete:
Do you really want to delete benchmark?