Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Direct call vs bind vs call vs apply vs static bound
(version: 0)
Comparing performance of:
direct call vs bind vs call vs apply vs static bind
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function test(msg) { var d = msg; } var testBound = test.bind(null);
Tests:
direct call
test("Hello");
bind
test.bind(null, "Hello")();
call
test.call(null, "Hello");
apply
test.apply(null, ["Hello"]);
static bind
testBound("Hello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
direct call
bind
call
apply
static bind
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; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
direct call
1648777600.0 Ops/sec
bind
139454880.0 Ops/sec
call
1648324992.0 Ops/sec
apply
308315616.0 Ops/sec
static bind
309263968.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The provided JSON defines a benchmark that tests different approaches to calling functions in JavaScript: 1. Direct call (`test("Hello");`) 2. Binding using `bind` method (`test.bind(null, "Hello")()`) 3. Binding using `call` method (`test.call(null, "Hello")`) 4. Applying the function using `apply` method (`test.apply(null, ["Hello"])`) 5. Static binding (using a bound function) (`testBound("Hello")`) **Options Compared** The benchmark compares the performance of these different approaches: * Direct call: The original way of calling a function. * Bind: A method that creates a new function with a specific `this` context. * Call: A method that calls a function with a specified `this` context and arguments. * Apply: A method that applies a function to an array of arguments, with the first argument as the `this` context. * Static bind: A bound function created using `bind`, which is stored in memory. **Pros and Cons** Here's a brief summary: * **Direct call**: Simple, but can lead to performance issues due to the overhead of creating a new function context. * **Bind**: Can be useful when you need to set the `this` context for a function, but creates a new function object that may have additional overhead. * **Call**: Similar to bind, but applies to an array of arguments. Can be slower than direct call due to the additional work. * **Apply**: Faster than call because it only needs to create a new execution context with the specified `this` and arguments. However, it can lead to issues when working with function pointers or multiple argument passing. * **Static bind**: The most efficient approach, as the bound function is stored in memory, avoiding the overhead of creating a new function object. **Library: Function** The benchmark uses JavaScript's built-in `Function` constructor and methods (`bind`, `call`, and `apply`) to test different approaches. No external libraries are required. **Special JS Feature/Syntax** This benchmark does not use any special features or syntax, such as async/await, generators, or closures. It only focuses on basic function calling mechanics. **Other Alternatives** Some alternative approaches to binding functions in JavaScript include: * **Arrow functions**: Create a new function with the `this` context set automatically using an arrow function. * **Prototypal inheritance**: Use inheritance patterns (e.g., prototypal subclassing) to set up the `this` context for a function. * **Function factories**: Create functions that return other functions, allowing you to control the `this` context. Keep in mind that these alternatives may have different performance characteristics or use cases compared to the binding methods used in this benchmark.
Related benchmarks:
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 vs prebind
Direct call vs saved bind vs inline bind vs call vs apply vs closure
Comments
Confirm delete:
Do you really want to delete benchmark?