Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function call vs bind vs call
(version: 0)
Comparing performance of:
Function vs Function bind vs Function call
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function makeNode() { return { valid: Math.random() > 0.5 }; } function isNodeValid( node ) { return node.valid; } function isValid() { return this.valid; } const nodes = []; for( let i = 0; i < 10000; i++ ) { const node = makeNode(); node.isValid = isValid.bind( node ); nodes.push( node ); } function getNodes() { return nodes; }
Tests:
Function
getNodes().forEach( node => isNodeValid( node ) );
Function bind
getNodes().forEach( node => node.isValid() );
Function call
getNodes().forEach( node => isValid.call( node ) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Function
Function bind
Function call
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Function
289773.8 Ops/sec
Function bind
12179.2 Ops/sec
Function call
189032.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents a JavaScript benchmark that tests the performance of three different approaches: calling a function as a standalone expression, binding a function to an object, and calling a bound function. **Options being compared** 1. **Function call**: The first option, "Function", calls the `isValid()` function directly on the `node` object. 2. **Binding a function**: The second option, "Function bind", binds the `isValid()` function to the `node` object using the `.bind()` method, so that it can access the `valid` property of the `node`. 3. **Bound function call**: The third option, "Function call", calls the bound `isValid()` function on the `node` object. **Pros and cons of each approach** 1. **Function call**: * Pros: Simple and straightforward. * Cons: May lead to unnecessary method calls on the `node` object, potentially introducing performance overhead due to method lookup. 2. **Binding a function**: * Pros: Allows the bound function to access the `valid` property of the `node` object without additional method calls. * Cons: May require more memory allocation for the bound function, as it creates an additional reference to the `isValid()` function. 3. **Bound function call**: * Pros: Combines the benefits of binding and calling a function directly on the `node` object. * Cons: May have performance overhead due to method lookup or binding. **Library usage** In this benchmark, no external libraries are used. The code relies solely on native JavaScript features. **Special JS feature/syntax** None mentioned in this specific benchmark. **Benchmark preparation code explanation** The script prepares an array of 10,000 objects with a `valid` property set to a random value between 0 and 1. Each object also has an `isValid()` method bound to it using `.bind()`. The `getNodes()` function returns the array of objects. **Individual test cases explanation** Each test case defines a different scenario: 1. **Function**: Calls `isNodeValid()` directly on each node in the array. 2. **Function bind**: Calls the bound `isValid()` function on each node in the array. 3. **Function call**: Calls the bound `isValid()` function on each node in the array. **Benchmark result interpretation** The benchmark result shows the average number of executions per second for each test case across multiple runs. The highest value indicates the fastest performance. In this case, "Function bind" appears to be the fastest option, followed closely by "Function call". The "Function" option is slower due to the potential overhead of method calls on the `node` object. **Alternative approaches** Other possible approaches could include: * Using arrow functions instead of traditional function expressions * Using `let/const` declarations with block scope instead of `var` * Optimizing memory allocation and garbage collection for the nodes array * Using parallel processing or multi-threading to execute the tests concurrently
Related benchmarks:
arrow vs bind
.bind() vs function
Function call vs bind vs bind and call vs function.call
Arrow function vs bind function2021-reznik
Comments
Confirm delete:
Do you really want to delete benchmark?