Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Bind vs arrow function instantiation performance
(version: 0)
Benchmark.js
is it faster to create functions using function.bind or to wrap them in an arrow function. tested for requestAnimationFrame generation
Comparing performance of:
Bind vs Arrow
Created:
4 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var ctx = { u: 123, x: 456, w(u) { return u + this.x; } };
Tests:
Bind
requestAnimationFrame(ctx.w.bind(ctx, ctx.u));
Arrow
requestAnimationFrame(() => { ctx.w(ctx.u); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Bind
Arrow
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Arrow
2.8M/s
Bind
2.0M/s
View exact numbers
Test name
Executions per second
✓
Arrow
2,751,798 Ops/sec
Bind
1,965,346 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Purpose:** The purpose of this benchmark is to compare the performance of two approaches: 1. **Function Bind**: Creating functions using `function.bind()`. 2. **Arrow Function**: Wrapping a function in an arrow function (`=>`). Both approaches are used to create a closure, which is a function that has access to its own scope and can capture variables from that scope. **Benchmarked Scenario:** The benchmark tests the creation of closures for a specific scenario: * A context object `ctx` with two properties: `u` and `x`. * A method `w` on `ctx`, which takes `u` as an argument and returns `u + x`. The benchmark measures the performance of creating a closure by binding the `w` method to the `ctx` object using either `function.bind()` or wrapping it in an arrow function. **Pros and Cons:** 1. **Function Bind**: * Pros: + Can be more readable, as the intention of creating a closure is explicit. + May perform better due to the optimizer's ability to inline bound functions. * Cons: + May require more boilerplate code, depending on the number of closures needed. 2. **Arrow Function**: * Pros: + More concise and readable, as the syntax for creating a closure is implicit. + Often faster, since the arrow function can be inlined by the optimizer. * Cons: + May be less explicit about the intention of creating a closure. **Other Considerations:** * Both approaches can capture variables from the surrounding scope using `this` or closures. In this benchmark, `ctx.u` is used to capture the value of `u`. * The benchmark uses `requestAnimationFrame()` to generate performance metrics, which may introduce some overhead. * Modern JavaScript engines have various optimizations for arrow functions and closures, so the actual performance difference between these approaches might be negligible in modern browsers. **Library Used:** There isn't a specific library used in this benchmark. However, it's worth noting that `requestAnimationFrame()` is a part of the Web API, which is maintained by W3C (World Wide Web Consortium). **Special JS Feature or Syntax:** The benchmark uses JavaScript features that are widely supported: * Function expressions and closures * Arrow functions (`=>`) * Method invocation on objects (`ctx.w`) If you're interested in exploring other alternatives, here are a few options: 1. **Using `const` instead of `var`**: In some cases, using `const` to declare variables can improve performance by preventing hoisting. 2. **Using `let` and `const` with blocks**: Using `let` and `const` with block statements (e.g., `if`/`else`) can help avoid scoping issues and improve readability. 3. **Optimizing closures**: Techniques like memoization, caching, or optimizing loop performance can have a significant impact on application performance. Keep in mind that the best approach depends on the specific use case and requirements of your project.
Related benchmarks
Arrow function vs bind function
Arrow function vs bind function dont bind
Arrow function vs bind function2021-reznik
Arrow function vs bind function creation
Comments
Confirm delete:
Do you really want to delete benchmark?