Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Direct call vs bind vs call vs apply (don't re-create bound function every time)
(version: 0)
Comparing performance of:
direct call vs bind vs call vs apply
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function test(msg) { var d = msg; } var boundTest = test.bind(null)
Tests:
direct call
test("Hello");
bind
boundTest("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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
direct call
476597696.0 Ops/sec
bind
51816028.0 Ops/sec
call
477984128.0 Ops/sec
apply
54209752.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 JSON** The provided JSON represents a benchmark test case on MeasureThat.net. The test compares the performance of four different approaches to invoke a function: `direct call`, `bind`, `call`, and `apply`. These approaches are used to create a bound version of the same function, which is created by binding the `null` value as the first argument. **Options Compared** The options compared in this benchmark test case are: 1. **Direct Call**: Invoking the function directly using its name. 2. **Bind**: Using the `bind()` method to create a bound version of the function. 3. **Call**: Invoking the bound function using its `call()` method. 4. **Apply**: Invoking the bound function using its `apply()` method. **Pros and Cons** Each approach has its own pros and cons: 1. **Direct Call**: * Pros: Simple, efficient, and widely supported. * Cons: May incur overhead due to the need for argument resolution and caching. 2. **Bind**: * Pros: Can be used to create a bound version of a function that takes an arbitrary number of arguments. * Cons: Creates a new function object each time it's called, which can lead to increased memory usage. 3. **Call**: * Pros: Efficient and widely supported. * Cons: Requires the target object to be available, and may incur overhead due to argument resolution. 4. **Apply**: * Pros: Can be used to invoke a bound function with a fixed set of arguments. * Cons: May incur overhead due to argument resolution and caching. **Library Used** There is no library explicitly mentioned in the provided JSON. However, it's likely that the `bind()`, `call()`, and `apply()` methods are part of the JavaScript Standard Library or a browser-specific implementation. **Special JS Feature or Syntax** The benchmark test case uses special syntax related to function invocation: * **`bind()`**: Creates a new bound version of a function. * **`call()`**: Invokes a bound function with a fixed set of arguments. * **`apply()`**: Invokes a bound function with an array of arguments. **Other Alternatives** There are alternative approaches to invoke functions, such as: 1. **Arrow Functions**: A concise syntax for defining small, anonymous functions. 2. **Async/Await**: A syntax for writing asynchronous code that's easier to read and maintain. 3. **Closures**: A way to create private variables and functions within a scope. However, these alternatives are not relevant to the specific benchmark test case provided. I hope this explanation helps you understand the JavaScript microbenchmark test case!
Related benchmarks:
Bind vs Direct
Direct call vs bind vs call vs apply vs prebind
Direct call vs saved bind vs inline bind vs call vs apply vs closure
Direct call vs bind vs call vs apply vs static bound
Comments
Confirm delete:
Do you really want to delete benchmark?