Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
another approach to Switch vs Object Literal
(version: 1)
Another approach to this test: https://www.measurethat.net/Benchmarks/Show/3444/0/switch-vs-object-literal
Comparing performance of:
Switch vs Object property vs Object with arrow funcion vs Object with funcion (from the link in description)
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
Switch
const useSwitch = (str) => { switch (str) { case 'a': return 'A'; break case 'b': return 'B'; break case 'c': return 'C'; break } } console.log(useSwitch(str));
Object property
const useObject = (str) => { let letters = { a: 'A', b: 'B', c: 'C' } return letters[str] } console.log(useObject(str));
Object with arrow funcion
var arrow = { a: () => 'A', b: () => 'B', c: () => 'C' } console.log(arrow[str]());
Object with funcion (from the link in description)
var objLiteral = { a: function() { console.log('A'); }, b: function() { console.log('B'); }, c: function() { console.log('C'); } } objLiteral[str]();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Switch
Object property
Object with arrow funcion
Object with funcion (from the link in description)
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):
Let's break down the provided benchmark JSON and explain what's being tested, compared, and discussed. **Benchmark Purpose:** The benchmark compares four approaches to access a value stored in an array or object: 1. Using a `switch` statement 2. Accessing an object property using bracket notation (`str['a']`, etc.) 3. Using an object with arrow functions (e.g., `arrow[str]()`) 4. Using an object with traditional function expressions (e.g., `objLiteral[str]()`) The benchmark aims to evaluate the performance of these four approaches on a JavaScript engine. **Comparison Options:** The benchmark compares the execution speed of each approach using various browsers and devices, including Chrome Mobile 79 running on Android 9. The comparison options are: * Execution speed (ExecutionsPerSecond) in the number of executions per second * Browser type and version * Device platform and operating system **Pros and Cons:** * **Switch statement**: Pros: Simple, concise syntax; Cons: May have performance overhead due to branching. * **Object property access**: Pros: Fast, efficient; Cons: May be slower than switch statements for large numbers of cases. * **Object with arrow functions**: Pros: Concise, modern syntax; Cons: May have compatibility issues in older browsers or environments. * **Object with traditional function expressions**: Pros: Widely supported and compatible; Cons: May be less concise than other approaches. **Library Usage:** None of the benchmark test cases use external libraries. The `useSwitch` and `objLiteral` functions rely on built-in JavaScript features, while `useObject` uses an object literal with bracket notation to access the property. **Special JS Features or Syntax:** None of the test cases use special JavaScript features like ES6 modules, async/await, or Promises. The focus is on evaluating the performance of basic syntax elements. **Alternatives:** If you're interested in exploring alternative approaches, here are some options: * Use a different browser or device platform to compare performance * Experiment with other languages or engines (e.g., Node.js, TypeScript) * Test additional features or optimizations (e.g., caching, parallel execution) * Explore more complex scenarios or edge cases By analyzing the benchmark's structure and comparison options, you can gain insights into the performance characteristics of each approach and make informed decisions about which syntax to use in your own JavaScript applications.
Related benchmarks:
Number vs + vs parseFloat
Number vs + vs parseInt
Math.floor vs Math.trunc
Number vs + vs parseFloat 23
12123Test
Comments
Confirm delete:
Do you really want to delete benchmark?