Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object function binding vs global scope lookup
(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(run); } };
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):
I'll break down the benchmark definition and explain what's being tested. **Benchmark Definition:** The benchmark is comparing two ways to call a function, `runGlobal()` and `runBoundOwn()`, which are properties of an object named `OBJECT`. **Options Compared:** 1. **Global Method Lookup**: This approach uses the dot notation (`.`) to access the `runGlobal()` method on the global object (i.e., the `OBJECT` instance). 2. **Bound Own Method**: This approach uses the bracket notation (`[]`) and the `this` keyword to access the `runBoundOwn()` method, which is a property of the `OBJECT` instance. **Pros and Cons:** 1. **Global Method Lookup** * Pros: + Easier to read and write + More intuitive for developers familiar with object notation * Cons: + May be slower due to lookup in the global object's property map (if the property is not cached) 2. **Bound Own Method** * Pros: + Can provide a performance boost by reducing the number of lookups required (as `this` already refers to the instance) * Cons: + Less intuitive for developers unfamiliar with bracket notation and `this` + May require additional setup or caching to optimize performance **Library:** There is no explicit library mentioned in this benchmark. However, the use of a custom object `OBJECT` with properties like `_buffer`, `_queue1`, and `_queue2` suggests that the benchmark is testing specific JavaScript engine optimizations. **Special JS Feature/Syntax:** This benchmark uses the bracket notation (`[]`) to access properties, which might be a feature-specific or implementation-dependent aspect of some JavaScript engines. However, without more context, it's difficult to pinpoint exactly what's being tested here. **Other Alternatives:** There are other ways to call functions on an object, such as: 1. **Dot notation with `in` operator**: `OBJECT.runGlobal()` 2. **Using `call()` or `apply()` methods**: `OBJECT.runBoundOwn.call(this)` 3. **Using a closure or function expression**: `(function() { return OBJECT.runGlobal(); })()` Keep in mind that the performance differences between these approaches can be significant, and this benchmark is likely testing specific optimizations or implementation details of certain JavaScript engines. The test cases are designed to measure the execution time of calling `runGlobal()` and `runBoundOwn()` using different methods. The results show that Chrome 96 performs better with `bound own method` (lower `ExecutionsPerSecond` value), which might be due to specific optimizations or caching in this browser implementation.
Related benchmarks:
closure lookup
bind vs inline func
function versus const arrow function
Nesting of functions in JavaScript
"this" property vs. closure upvalue
Comments
Confirm delete:
Do you really want to delete benchmark?