Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Class method / instance method / bind this – closer to reality test
(version: 0)
Comparing performance of:
Call class method vs Call instance method vs Call bound method
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.A = class { a() {} } window.AReady = new A(); window.B = class { b = () => {}; } window.BReady = new B(); window.C = class { constructor() { this.c = this.c.bind(this); } c() {} } window.CReady = new C();
Tests:
Call class method
AReady.a();
Call instance method
BReady.b();
Call bound method
CReady.c();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Call class method
Call instance method
Call bound method
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 test cases. **What is being tested?** The benchmark measures the performance of JavaScript methods on three different approaches: 1. **Class method**: A class method is a function that belongs to a class and can be called on instances of that class. In this case, it tests `AReady.a()`, which calls the `a()` method on the instance `window.AReady`. 2. **Instance method**: An instance method is a function that belongs to an object and can be called directly on that object. In this case, it tests `BReady.b()`, which calls the `b()` method on the instance `window.BReady`. 3. **Bound method**: A bound method is an instance method that has been bound to a specific context using the `bind()` method. In this case, it tests `CReady.c()`, which calls the `c()` method on the instance `window.CReady` with `this` bound to itself. **Options compared** The three approaches are compared in terms of their performance. The benchmark is likely to be faster for one or more of these approaches, depending on various factors such as the language implementation, browser engine, and hardware. **Pros and cons of each approach:** 1. **Class method**: This approach can be beneficial when: * You need to call a function that belongs to a class, but you don't have an instance of that class. * The function is static (i.e., it doesn't depend on the state of the class). However, this approach can lead to slower performance compared to bound methods or direct method calls. 2. **Instance method**: This approach can be beneficial when: * You need to call a function that belongs to an object. * The function depends on the state of the object. However, this approach can lead to slower performance compared to bound methods. 3. **Bound method**: This approach can be beneficial when: * You need to call a function that belongs to an instance, but you want to bind it to a specific context (e.g., `this`). * The function depends on the state of the object. This approach is often faster than direct method calls because it avoids creating a new scope for the bound function. **Libraries and special JavaScript features** In this benchmark, no libraries are used. However, some special JavaScript features are employed: 1. **Arrow functions**: The `b()` method in the `BReady` class uses an arrow function (`=> {}`), which is a shorthand way to define small, anonymous functions. 2. **Class syntax**: The code uses modern JavaScript class syntax (e.g., `class { ... }`) to define classes. **Other alternatives** To test these approaches, alternative methods can be used: 1. **Using `apply()` or `call()`**: Instead of calling instance methods directly (`BReady.b()`), you can use `apply()` or `call()` to invoke a method with a specific context. 2. **Using function pointers**: You can use function pointers (i.e., variables that hold the address of a function) to call bound methods. Here's an example using `apply()`: ```javascript BReady.b.apply(BReady); ``` And here's an example using function pointers: ```javascript const b = BReady.b; b(); ``` Keep in mind that these alternatives may have different performance characteristics compared to the original approaches.
Related benchmarks:
Test private variables vs weakmap
Direct vs Binded Call
Class method / instance method / bind this
binded vs arrow
Comments
Confirm delete:
Do you really want to delete benchmark?