Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if else vs runtime polymorphic
(version: 0)
Comparing performance of:
if else vs polymorphic vs a_foo
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class Base { foo () {} } class A extends Base { foo () {} } class B extends Base { foo () {} } var a = new A() var b = new B() function a_foo () {} function b_foo () {} var type = 'componenta'
Tests:
if else
if (type === 'componenta') { a_foo() }
polymorphic
a.foo()
a_foo
a_foo()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if else
polymorphic
a_foo
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 JSON data and explain what is tested, compared, and discussed in this benchmark. **Benchmark Overview** The benchmark compares three different approaches to invoke a method on an object: 1. **If-Else Statement**: `if (type === 'componenta') { a_foo() }` 2. **Polymorphic Call**: `a.foo()` 3. **Direct Method Invocation**: `a_foo()` **Library and Purpose** The benchmark uses a JavaScript class hierarchy with two subclasses, `A` and `B`, which extend a base class `Base`. The purpose of this library is to demonstrate the differences in invoking methods on objects based on their type. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax that are unique to a specific browser or engine. However, it's worth noting that the benchmark uses a non-standard way of specifying the `type` variable as a string literal `'componenta'`. This is a common pattern in JavaScript development. **Approaches Compared** The three approaches compared in this benchmark: 1. **If-Else Statement**: Verifies that the code checks the `type` property and invokes the correct method (`a_foo()` or `b_foo()`) based on its value. 2. **Polymorphic Call**: Tests that the code uses polymorphism to invoke the correct method on an object, regardless of its type. 3. **Direct Method Invocation**: Demonstrates a straightforward way to invoke a method directly on an object without any checks. **Pros and Cons** Here are some pros and cons for each approach: 1. **If-Else Statement** * Pros: Easy to understand and maintain; can be used in various contexts. * Cons: Can lead to performance issues if the condition is expensive to evaluate or if there are many branches. 2. **Polymorphic Call** * Pros: Can improve performance by avoiding explicit type checks; works well with object-oriented programming principles. * Cons: May not be suitable for cases where the method implementation depends on the specific type, as it can lead to implicit typing issues. 3. **Direct Method Invocation** * Pros: Simple and straightforward; no overhead from type checking or branching. * Cons: Limited in its flexibility; requires explicit knowledge of the object's type. **Other Considerations** When evaluating these approaches, consider factors like: * Code readability and maintainability * Performance implications (e.g., branch prediction, cache misses) * Compatibility with different browsers and engines * Use cases where the approach might be more or less suitable Keep in mind that this benchmark is focused on demonstrating the differences between these three approaches rather than providing a comprehensive evaluation of their performance. **Alternatives** Other alternatives to consider when invoking methods on objects include: * Using `instanceof` operator to check the object's type * Employing a factory function or constructor to create objects with specific properties or behavior * Utilizing a more advanced programming paradigm, such as aspect-oriented programming (AOP) or functional programming
Related benchmarks:
Perfomance: Switch vs Polymorphism
instanceOf-bitwise
Comparison of classes vs prototypes vs object literals also including the inheritance
member vs proto member
Comments
Confirm delete:
Do you really want to delete benchmark?