Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function call vs bind vs bind and call vs function.call
(version: 0)
Comparing performance of:
Function vs Function bind vs Function bind and call 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 bind and call
getNodes().forEach( node => isValid.bind( node )() );
Function call
getNodes().forEach( node => isValid.call( node ) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Function
Function bind
Function bind and call
Function call
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Function
273630.4 Ops/sec
Function bind
20312.4 Ops/sec
Function bind and call
31574.7 Ops/sec
Function call
167447.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of three different approaches to calling functions in JavaScript: 1. **Direct Function Call**: `getNodes().forEach( node => isNodeValid( node ) );` 2. **Function Bind**: `getNodes().forEach( node => node.isValid.bind( node )() );` 3. **Function Bind and Call**: `getNodes().forEach( node => isValid.bind( node )() );` These approaches are being compared in the following test cases: * `Function`: Direct function call * `Function bind`: Function bind with a direct call ( `()` ) * `Function bind and call`: Using `bind` to create a new function and then calling it directly **What's Being Tested** The benchmark is testing the performance of each approach in terms of the number of executions per second. The goal is to determine which approach is the most efficient. **Pros and Cons of Each Approach** 1. **Direct Function Call (Function)** * Pros: Simple, straightforward, no overhead from binding. * Cons: May involve more function lookups and calls due to the `this` context. 2. **Function Bind** * Pros: Reduces function lookups by creating a new bound function. * Cons: Requires an additional call to execute the bound function. 3. **Function Bind and Call** * Pros: Combines the benefits of binding and direct calling, reducing overhead. * Cons: May still involve some function lookups due to the `this` context. **Library Usage** The benchmark uses the `isValid` function, which is not defined in the provided code snippet. However, based on the usage, it appears that `isValid` is a function that checks whether a node's `valid` property has been set. The exact implementation of `isValid` is not relevant to the performance comparison being made. **Special JS Feature or Syntax** There are no specific JavaScript features or syntaxes used in this benchmark beyond what's already discussed. However, it's worth noting that using `bind` and `call` can be a common pattern in JavaScript when working with functions and their context. **Other Alternatives** If the goal is to optimize function call performance, other approaches might include: * **Caching**: Storing the results of expensive function calls to avoid repeating them. * **Memoization**: Storing the results of function calls so that they can be reused instead of recalculated. * **Inlining**: Inline-fusing functions to reduce overhead and improve performance. However, these optimizations are typically more complex and not directly related to the simple comparison being made in this benchmark.
Related benchmarks:
arrow vs bind
.bind() vs function
Function call vs bind vs call
Arrow function vs bind function2021-reznik
Comments
Confirm delete:
Do you really want to delete benchmark?