Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object function binding vs closure plus cache + execution
(version: 0)
Comparing performance of:
global method lookup vs bound own method
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const QUEUE_1 = new Map([ [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}] ]); const QUEUE_2 = new Map([ [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}] ]); let BUFFER = -1; function run() { QUEUE_1.clear(); QUEUE_2.clear(); } let c = 0; const cancel = function(n) { c--; }; const request = function(fn) { fn(); return ++c; }; var OBJECT = { _buffer: -1, _queue1: new Map([ [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}] ]), _queue2: new Map([ [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}], [{}, {}] ]), runGlobal() { cancel(BUFFER); BUFFER = request(run); }, runBoundOwn() { cancel(this._buffer); this._buffer = request(this._run); }, _run() { this._queue1.clear(); this._queue2.clear(); } }; OBJECT._run = OBJECT._run.bind(OBJECT);
Tests:
global method lookup
OBJECT.runGlobal();
bound own method
OBJECT.runBoundOwn();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
global method lookup
bound own method
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark measures the performance difference between two approaches: global method lookup and bound own method. **What are we testing?** We're testing the efficiency of calling methods on an object, specifically: 1. `OBJECT.runGlobal()`: This function calls another function `run` from the same object using a global reference (`OBJECT`). The method is looked up by name in the object. 2. `OBJECT.runBoundOwn()`: This function calls another function `run` from the same object using a bound reference (`this`). The method is looked up by name in the object, but with the additional overhead of binding to a specific context. **Options compared** The benchmark compares two approaches: 1. **Global Method Lookup**: Calling `OBJECT.runGlobal()` which uses a global reference to look up and call the `run` function. 2. **Bound Own Method**: Calling `OBJECT.runBoundOwn()` which uses a bound reference to look up and call the `run` function. **Pros and Cons** * **Global Method Lookup**: + Pros: Simple, straightforward approach that doesn't require any extra setup or context binding. + Cons: May incur additional overhead due to searching for the method name in the object's prototype chain. * **Bound Own Method**: + Pros: Can be faster since it uses a bound reference which eliminates the need for dynamic method lookup. However, it requires more setup (binding the `run` function to the object). + Cons: Requires additional code and context setup, which may introduce overhead. **Library/Functionality usage** In this benchmark, we're using a custom object `OBJECT` with two methods: `runGlobal()` and `runBoundOwn()`. These methods are used to call another function `run`, but the implementation details are not shown. The library used is the built-in JavaScript `Map` data structure for storing queues. **Special JS feature** No special JavaScript features or syntax are mentioned in this benchmark, so we won't explore those further. **Other alternatives** If you're looking to optimize method lookup performance in JavaScript, consider using: * **`bind()`**: As shown in the benchmark, binding a function to an object can provide faster lookups. However, be aware of potential side effects and ensure that the bound context is properly managed. * **`lookupFunction()`**: In some JavaScript environments (e.g., Node.js), you can use `lookupFunction()` to look up methods in an object more efficiently than using a global reference. * ****Proxy** objects: Proxies provide a powerful way to manipulate and optimize method lookup. However, they introduce additional complexity and may not be suitable for all use cases. Keep in mind that the performance differences between these approaches can vary depending on the specific JavaScript engine, implementation, and context.
Related benchmarks:
bind-vs-arrow
invoke bound function vs invoke closure
bind vs inline func
Direct call vs bind vs call vs apply spred
bind vs closure
Comments
Confirm delete:
Do you really want to delete benchmark?