Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Direct call vs bind vs call vs apply (with precreated arg variables)
(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 arg = "Hello"; var args = [arg];
Tests:
direct call
test(arg);
bind
test.bind(null, arg)();
call
test.call(null, arg);
apply
test.apply(null, args);
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:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Goanna/6.7 Firefox/115.0 PaleMoon/33.7.1
Browser/OS:
Pale Moon (Firefox Variant) 33 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
direct call
175820912.0 Ops/sec
bind
6552970.0 Ops/sec
call
175210256.0 Ops/sec
apply
53861428.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark tests the performance of different ways to invoke a function in JavaScript: direct call, bind, call, and apply. **What is tested?** The test cases compare the execution speed of the following functions: 1. `test(arg);` (direct call): This is the original function invocation without any additional setup. 2. `test.bind(null, arg)();` (bind): This uses the `bind()` method to create a new function that invokes `test` with `arg` as its first argument. 3. `test.call(null, arg);` (call): This uses the `call()` method to invoke `test` with `arg` as its first argument. 4. `test.apply(null, args);` (apply): This uses the `apply()` method to invoke `test` with `args` as an array of arguments. **Options compared** The benchmark compares the performance of these four options: 1. Direct call (`test(arg)`) 2. Bind (`test.bind(null, arg)()`) 3. Call (`test.call(null, arg)`) 4. Apply (`test.apply(null, args)`) **Pros and cons of each approach:** 1. **Direct call (test(arg))**: * Pros: Simple, easy to understand. * Cons: May not be the most efficient way to invoke a function, especially if it's called repeatedly with different arguments. 2. **Bind (`test.bind(null, arg)()`)**: * Pros: Allows for more control over the function invocation, e.g., changing the context or adding additional arguments. * Cons: Creates a new function object each time bind is used, which can lead to memory allocation and garbage collection overhead. 3. **Call (`test.call(null, arg)`)**: * Pros: Similar to direct call but allows for more control over the context in which the function is invoked. * Cons: May not be as efficient as direct call if the function is called repeatedly with different arguments. 4. **Apply (`test.apply(null, args)`)**: * Pros: Allows for invoking a function with an array of arguments and setting the context explicitly. * Cons: Can lead to performance overhead due to the creation of a new function object. **Library used** None (no external libraries are mentioned in the provided code). **Special JS feature or syntax** The benchmark uses arrow functions (`test.bind(null, arg)`), which are supported since ECMAScript 2015. However, this is not explicitly highlighted as a special feature or syntax, and its use is straightforward for anyone familiar with JavaScript. **Other alternatives** In addition to the four options tested in the benchmark, other ways to invoke a function in JavaScript include: 1. `func()` (a function call like direct call) 2. `new func(arg)` (creating a new instance of the function using the constructor syntax) 3. Using a function expression with parentheses (`(function() { ... })()`) These alternatives are not included in the benchmark, and their performance characteristics may vary depending on the specific use case. Overall, the benchmark provides a good insight into the performance differences between various ways to invoke functions in JavaScript, which can be useful for optimizing code or understanding performance bottlenecks.
Related benchmarks:
.bind() vs function
Direct call vs bind vs call vs apply foosssdsad
Direct call vs bind vs call vs apply foosssdsad aas
Direct call vs bind vs call vs apply foosssdsad aasa
Comments
Confirm delete:
Do you really want to delete benchmark?