Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function override vs assignment
(version: 0)
Comparing performance of:
Override vs assignment
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Override
class BaseClass { a construct(a) { this.a = a } performTask(b) { return this.a + b; } } class ActualClass extends BaseClass { performTask(b) { return this.a * b; } } const random1 = Math.random() * 50 const random2 = [...Array(100)].map(v => Math.random() * 10) const expectedResult = random2.reduce((acc, cur, i) => acc + ((i % 2) * 2 - 1) * random1 * cur, 0) const instance = new ActualClass(random1) let result = 0 for (i = 0; i < random2.length; ++i) { result += ((i % 2) * 2 - 1) * instance.performTask(random2[i]) } console.log(result == expectedResult)
assignment
class ActualClass2 { a construct(a) { this.a = a } performTask = (a, b) => a + b } const random1 = Math.random() * 50 const random2 = [...Array(100)].map(v => Math.random() * 10) const expectedResult = random2.reduce((acc, cur, i) => acc + ((i % 2) * 2 - 1) * random1 * cur, 0) const instance = new ActualClass2(random1) instance.performTask = (a, b) => a * b let result = 0 for (i = 0; i < random2.length; ++i) { result += ((i % 2) * 2 - 1) * instance.performTask(random2[i]) } console.log(result == expectedResult)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Override
assignment
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):
I'll break down the provided benchmark for you. **Benchmark Overview** The benchmark measures the performance difference between two approaches to override the `performTask` method in a JavaScript class: function overriding and assignment. **Function Overriding vs Assignment** 1. **Function Overriding** In this approach, the `performTask` method is overridden by creating an instance of the `ActualClass` with the `a` property set to a random value. The `performTask` method is then called on each element of the `random2` array, and the results are accumulated. Pros: * Allows for more control over the method's behavior * Can lead to more efficient execution if the overridden method is optimized Cons: * Requires creating an instance of the class before calling the method * May incur overhead due to object creation and lookup 2. **Assignment** In this approach, the `performTask` property is assigned a new function that takes two arguments, `a` and `b`. The same instance is used throughout the test, and the `performTask` property is reassigned with a new function after its initial assignment. Pros: * Reuses the existing instance without creating a new one * May be faster due to reduced object creation overhead Cons: * Requires explicit reassignment of the `performTask` property for each execution * May lead to slower performance if the new function is not optimized **Library: None** Neither of the test cases relies on any external libraries. **Special JS Features/Syntax: None** There are no special JavaScript features or syntax used in these test cases, such as async/await, generators, or decorators. **Benchmark Preparation Code** The preparation code for each benchmark is empty, which means that the test cases start from a clean slate without any additional setup or teardown. **Alternative Approaches** Other approaches to testing function overriding vs assignment could include: * Using a mock framework like Jest or MockJS to isolate dependencies and improve testability * Creating multiple instances of the `ActualClass` for each execution to eliminate instance creation overhead * Using a different data structure, such as an array of functions, instead of an object with a single property However, these alternative approaches may not be necessary depending on the specific requirements and goals of the benchmark.
Related benchmarks:
Closure plus class check versus function apply
Class instance method lookup vs function-created object method lookup (fork)
arrow vs function test lalala
Property assignment vs Variable assignment
Object Assignment vs DefineProperties
Comments
Confirm delete:
Do you really want to delete benchmark?