Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
invoke bound function vs invoke closure
(version: 1)
Comparing performance of:
invoke bound vs invoke closure vs invoke boundThis
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = {v : Math.random()} var bound = (function(a_) { a_.v = Math.imul(2, a_.v); }).bind(null, a); var boundThis = (function() { this.v = Math.imul(2, this.v); }).bind(a); var closure = (function() { const a_ = a; return function() { a_.v = Math.imul(2, a_.v); }; })();
Tests:
invoke bound
bound();
invoke closure
closure();
invoke boundThis
boundThis();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
invoke bound
invoke closure
invoke boundThis
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Android 10; Mobile; rv:128.0) Gecko/128.0 Firefox/128.0
Browser/OS:
Firefox Mobile 128 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
invoke bound
4869898.5 Ops/sec
invoke closure
21851992.0 Ops/sec
invoke boundThis
4846871.0 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 Overview** The benchmark tests three different approaches to invoking functions: `bound()`, `closure()`, and `boundThis()` (also referred to as "invoke boundThis"). The goal is to compare their performance in a JavaScript environment. **Script Preparation Code** The script preparation code defines a test object `a` with a property `v`. Two functions are defined: 1. `bound`: A function that takes an argument `a_`, binds it to the `a` object, and returns a new function. The bound function multiplies its input by 2 and assigns the result to `v`. 2. `closure`: A function that returns another function. This returned function also takes no arguments, but uses the `this` keyword to access the `v` property of the outer scope (i.e., the original `a` object). 3. `boundThis`: Similar to `bound`, but binds the function to the `a` object explicitly. **Test Cases** The benchmark consists of three test cases: 1. `invoke bound`: Calls the `bound()` function and measures its performance. 2. `invoke closure`: Calls the `closure()` function and measures its performance. 3. `invoke boundThis`: Calls the `boundThis()` function and measures its performance. **Library and Syntax** In this benchmark, we don't see any explicit use of a library beyond the built-in JavaScript features. However, it's worth noting that the use of `this` to access outer scope variables is a common pattern in JavaScript closures. **Options Compared** The three test cases are compared based on their invocation strategy: * `bound()`: The function is bound to the `a` object using the `.bind(null, a)` method. This approach is often used when you want to ensure that the function always receives the original context (`this`) as its first argument. * `closure()`: The function returns another function that uses the outer scope's `v` property directly. This approach relies on JavaScript's lexical scoping rules, where the returned function inherits the scope of the outer function. * `boundThis`: Similar to `bound()`, but explicitly binds the function to the `a` object using the `.bind(a)` method. **Pros and Cons** Here are some pros and cons for each approach: 1. **bound()**: * Pros: Easy to understand, clear intent. * Cons: May incur additional overhead due to binding. 2. **closure()**: * Pros: Lightweight, no binding overhead. * Cons: May lead to unexpected behavior if not used carefully (e.g., accessing outer scope variables directly). 3. **boundThis**: * Pros: Similar benefits to `bound()` with the added clarity of explicit binding. * Cons: Additional method call overhead compared to `closure()`. **Other Alternatives** Other approaches might be used depending on the specific use case: 1. Using an arrow function (`() => { ... }`) instead of a traditional function declaration might reduce overhead for both `bound` and `boundThis`. 2. Using a `const` or `let` variable for the `v` property in the outer scope might affect performance. Keep in mind that these are general considerations, and actual performance differences will depend on various factors, such as JavaScript engine optimization and specific use cases.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
Assignment of value vs Destructuring an object with random
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance 1_1
Comments
Confirm delete:
Do you really want to delete benchmark?