Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Direct call vs bind vs call vs apply in classes
(version: 0)
Comparing performance of:
direct call vs bind vs call vs apply
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class Test { msg = 'hello' test(more) { return this.msg + more } } var t = new Test() var orig = t.test var bound = orig.bind(t)
Tests:
direct call
t.test("world");
bind
bound('world')
call
orig.call(t, "world");
apply
orig.apply(t, ["world"]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
direct call
bind
call
apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
direct call
21160562.0 Ops/sec
bind
21429372.0 Ops/sec
call
10341995.0 Ops/sec
apply
10376101.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance difference between four approaches to invoke a method `test` on an object `t`: 1. **Direct Call**: `t.test("world");` 2. **Bind**: `bound('world')`, where `bound` is a function created by binding `orig` (the original method) to the context of `t`. 3. **Call**: `orig.call(t, "world")`, where `orig` is called with the object `t` as its first argument and the string `"world"` as its second argument. 4. **Apply**: `orig.apply(t, ["world"])`, similar to call, but using an array instead of positional arguments. **Options Compared** The benchmark compares the performance of these four approaches: * **Pros and Cons:** + **Direct Call**: Fastest, since it directly invokes the method on the object. However, it may have type errors if `t.test` is not a function. + **Bind**: May be slower due to the overhead of creating a bound function. However, it ensures that the method is called with the correct context, avoiding potential type errors. + **Call** and **Apply**: Similar in performance, as they both use the same mechanism (function invocation) and are generally faster than bind. However, call may be slightly slower due to the array creation overhead in apply. **Libraries and Features** There is no explicit library mentioned in the benchmark definition. However, it uses JavaScript features like: * **Closures**: The `bound` function creates a closure over the `orig` method, ensuring that it is called with the correct context. * **Function Invocation**: The benchmark uses various ways to invoke the `test` method on the object `t`, showcasing different invocation patterns. **Other Considerations** When writing benchmarks like this, consider the following: * Use meaningful names for variables and functions to make the code easy to understand. * Ensure that the benchmark is representative of real-world use cases or scenarios. * Use a consistent naming convention throughout the code. * Keep the benchmark definition concise and focused on the key aspects being measured. **Alternative Benchmarks** If you wanted to compare these approaches with others, you might consider: * Using a different method invocation pattern, such as using `this` or an arrow function instead of bind. * Incorporating additional factors, like object creation overhead or garbage collection pauses. * Measuring performance in a different context, like a loop or recursive function call. * Comparing the performance of these approaches with other languages or implementations.
Related benchmarks:
.bind() vs function
Bind vs Direct
Direct call vs saved bind vs inline bind vs call vs apply vs closure
Class vs bind vs hook
Comments
Confirm delete:
Do you really want to delete benchmark?