Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
this vs variable containing this
(version: 0)
Comparing performance of:
this vs t = this
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
aClass = class { constructor() { this.a = 1; } usethis() { this.a = this.a ? 0 : 1; this.a = this.a ? 0 : 1; this.a = this.a ? 0 : 1; this.a = this.a ? 0 : 1; this.a = this.a ? 0 : 1; } usevar() { const t = this; t.a = t.a ? 0 : 1; t.a = t.a ? 0 : 1; t.a = t.a ? 0 : 1; t.a = t.a ? 0 : 1; t.a = t.a ? 0 : 1; } } inst = new aClass;
Tests:
this
inst.usethis();
t = this
inst.usevar();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
this
t = this
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
this
3802553.5 Ops/sec
t = this
3780281.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark. **Benchmark Definition** The test is designed to measure the performance difference between two approaches: 1. `this` (Direct Access) 2. `t = this` (Binding the reference using assignment) In JavaScript, when you use `this` directly in a function call, it refers to the current instance of the class. However, if you assign the result of `this` to a new variable (`t`) and then use that variable, it creates an additional layer of indirection. **Options Compared** The two approaches being compared are: 1. **Direct Access**: `inst.usethis()` * Pros: Less overhead due to no assignment of `this`. * Cons: May lead to faster execution, but also may be more difficult to understand and debug. 2. **Binding the reference using assignment**: `t = this; inst.usevar()` * Pros: Easier to read and understand, as it's explicitly binding the reference to a new variable. * Cons: More overhead due to the additional assignment of `this`, which may slow down execution. **Library Used** In this benchmark, no external library is used. The test only utilizes built-in JavaScript features. **Special JS Feature/Syntax** The special feature used in this benchmark is the concept of **"the context"** or **"this binding"**, which determines how `this` refers to an object within a function call. When using `this` directly, it inherits the value from the surrounding scope, whereas when assigning it to a new variable, it creates a new scope with its own value. **Benchmark Preparation Code** The provided code defines a class `aClass` with two methods: `usethis()` and `usevar()`. Both methods perform an assignment operation on `this.a`, which is initialized to 1 in the constructor. The difference lies in how `this` is accessed within each method: * `usethis()`: Direct access using `this`. * `usevar()`: Binding the reference using assignment: `t = this;`. **Other Considerations** This benchmark tests the performance impact of the binding approach, which may be relevant when working with large datasets or performance-critical code. However, it's essential to note that the difference between these two approaches is relatively small and may not significantly affect performance in most cases. If you're interested in exploring other alternatives, consider the following: * Using a different binding mechanism, such as using an arrow function (`=>`) instead of assigning `this` to a variable. * Optimizing the code for specific use cases or performance metrics (e.g., using caching or memoization). * Investigating the impact of other factors on performance, such as garbage collection or CPU architecture. Keep in mind that benchmarking JavaScript performance can be complex and influenced by various factors, including browser engines, hardware, and system configuration.
Related benchmarks:
Class instance method lookup vs function-created object method lookup (fork)
object.assign vs new class extends
function vs class method vs new function method v2
class scope access - cache vs this
Comments
Confirm delete:
Do you really want to delete benchmark?