Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Direct call vs bind vs call vs apply (with specified object)
(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:
window.obj = { prop: '', f: function() {} }; function test(msg) { var d = msg; }
Tests:
direct call
test("Hello");
bind
test.bind(window.obj, "Hello")();
call
test.call(window.obj, "Hello");
apply
test.apply(window.obj, ["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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
direct call
495122784.0 Ops/sec
bind
58757496.0 Ops/sec
call
460036736.0 Ops/sec
apply
471938208.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is defined by a single script that creates an object `window.obj` with two properties: `prop` and `f`. The script then defines a function `test(msg)` that takes a message `msg`. **Test Cases** There are four test cases, each defining a different way to call the `test` function: 1. **Direct Call**: `test("Hello");` 2. **Bind**: `test.bind(window.obj, "Hello")();` 3. **Call**: `test.call(window.obj, "Hello");` 4. **Apply**: `test.apply(window.obj, ["Hello"]);` These test cases are designed to compare the performance of each method. **Options Compared** The options being compared are: * Direct Call (`test("Hello")`) * Bind (`test.bind(window.obj, "Hello")()`) * Call (`test.call(window.obj, "Hello")`) * Apply (`test.apply(window.obj, ["Hello"])`) These methods differ in how they bind the `window.obj` object to the `test` function. **Pros and Cons** Here's a brief summary of each method: 1. **Direct Call**: Simplest approach, but may incur overhead due to string concatenation. * Pros: Easy to implement, no additional overhead. * Cons: May be slower than other methods. 2. **Bind**: Creates a new bound function that wraps the original `test` function with the provided object as its first argument. * Pros: Can provide more control over the binding process, may be faster than direct call due to reduced string concatenation overhead. * Cons: Requires an extra step to create the bound function. 3. **Call**: Calls the `test` function directly on the provided object, passing the message as an argument. * Pros: Similar to bind but with less overhead, as it avoids creating a new function. * Cons: May be slower than direct call due to additional argument passing. 4. **Apply**: Applies the `test` function to the provided object and arguments using the `apply()` method. * Pros: Can provide more control over the binding process, may be faster than direct call or bind due to reduced overhead. * Cons: Requires an extra step to create a new array of arguments. **Other Considerations** The benchmark is likely designed to test the performance differences between these methods on different browsers and devices. The use of `window.obj` as a binding object suggests that the benchmark wants to isolate the performance differences due to function call semantics, rather than other factors like memory allocation or garbage collection. **Alternatives** If you wanted to create a similar benchmark, you could consider adding more test cases, such as: * Using an array of arguments instead of `apply()` * Using a different binding object or syntax * Adding additional functions to the `test` function to increase complexity However, keep in mind that adding too many test cases may lead to increased overhead and decreased accuracy. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
Object.assign vs direct copy
in vs direct call
Object.assign vs direct assign
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Comments
Confirm delete:
Do you really want to delete benchmark?