Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Direct call vs bind vs call vs apply
(version: 0)
Comparing performance of:
direct call vs bind vs call vs apply
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function test(msg) { var d = msg; }
Tests:
direct call
test("Hello");
bind
test.bind(null, "Hello")();
call
test.call(null, "Hello");
apply
test.apply(null, ["Hello"]);
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:
6 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 141 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
direct call
133091152.0 Ops/sec
bind
116205592.0 Ops/sec
call
119353328.0 Ops/sec
apply
108490056.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of four different ways to invoke a function: `direct call`, `bind`, `call`, and `apply`. The test is designed to measure which approach is fastest. **Test Cases** Each test case defines a specific way to invoke the `test` function, which is initially defined with a simple script preparation code: ```javascript function test(msg) { var d = msg; } ``` The test cases are: 1. **Direct Call**: `test("Hello");` 2. **Bind**: `test.bind(null, "Hello")();` 3. **Call**: `test.call(null, "Hello");` 4. **Apply**: `test.apply(null, ["Hello"]);` **Comparison** When the `bind`, `call`, and `apply` methods are invoked, they modify the behavior of the `test` function. Here's what happens: * **Bind**: The `bind` method sets the context (this) of the function to a specific value (`null` in this case). When the bound function is called, it will execute with that context. * **Call**: The `call` method sets the context (this) of the function to a specific value (`null` in this case) and passes an argument list (`"Hello"`). * **Apply**: The `apply` method sets the context (this) of the function to a specific value (`null` in this case) and passes an argument array (`["Hello"]`) as its first argument. **Library** The test cases use the built-in JavaScript methods `bind`, `call`, and `apply`. These methods are part of the ECMAScript standard and are implemented by most modern browsers. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Direct Call**: Fastest, but requires passing arguments explicitly. No overhead from method invocation. * **Bind**: More verbose than direct call, but allows for more flexibility in function invocation. Sets context (this) to specified value. * **Call**: Similar to bind, but passes argument list explicitly. Can be faster than bind if the context is already set correctly. * **Apply**: Most verbose of all methods, as it requires passing an array of arguments. However, can be useful when working with functions that require multiple arguments. **Considerations** When choosing between these approaches, consider the trade-offs: * Speed: Direct call is generally fastest. * Flexibility: Bind and call offer more flexibility in function invocation. * Verbose: Apply is the most verbose method. **Alternatives** Other alternatives to this benchmark could include: * Using a different function invocation syntax, such as arrow functions (`() => { ... }`) or generators/iterators. * Measuring performance with different JavaScript engines (e.g., V8 vs. SpiderMonkey). * Adding additional variables or dependencies to the test cases to measure their impact on performance. Overall, this benchmark provides a useful comparison of function invocation methods in JavaScript, helping developers understand the trade-offs involved in choosing the right approach for their use case.
Related benchmarks:
.bind() vs function
Bind vs Direct
Direct call vs bind vs call vs apply (don't re-create bound function every time)
Direct call vs bind vs call vs apply (with precreated arg variables)
Direct call vs saved bind vs inline bind vs call vs apply vs closure
Comments
Confirm delete:
Do you really want to delete benchmark?