Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
this vs that
(version: 0)
What is faster? Referencing something with this or with a variable called that, pointing to this?
Comparing performance of:
this vs that
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Monster() { this.x = 100; this.y = 100; console.log("Monster created at " + this.x + " / " + this.y + "!"); } function Monster2() { var that = this; that.x = 100; that.y = 100; console.log("Monster created at " + that.x + " / " + that.y + "!"); }
Tests:
this
new Monster();
that
new Monster2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
this
that
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
this
118944.8 Ops/sec
that
117272.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined as two test cases: 1. `new Monster();` 2. `new Monster2();` These test cases create instances of the `Monster` function and measure their performance. **Script Preparation Code** The script preparation code for both test cases is identical: ```javascript function Monster() { this.x = 100; this.y = 100; console.log("Monster created at " + this.x + " / " + this.y + "!"); } function Monster2() { var that = this; that.x = 100; that.y = 100; console.log("Monster created at " + that.x + " / " + that.y + "!"); } ``` Both test cases create an object with `x` and `y` properties, log a message to the console, and set `this` (or `that`) to point to the newly created object. **Options Compared** The benchmark compares two options: 1. **Using `this`**: In the first test case (`new Monster();`), the code uses `this` to access the object's properties. 2. **Using a variable named "that"**: In the second test case (`new Monster2();`), the code uses a variable named `that` (instead of `this`) to access the object's properties. **Pros and Cons** Here are some pros and cons for each approach: **Using `this`:** Pros: * More concise and readable * Faster lookup time in the object prototype chain Cons: * May be slower due to the overhead of accessing `this` (depending on the browser) **Using a variable named "that"**: Pros: * Avoids potential issues with `this` being overridden by other variables or functions * May be faster due to the compiler optimizing the lookup time for `that` Cons: * Less concise and more verbose * Requires declaring an additional variable (`that`) that may consume more memory **Library Used** The benchmark uses a library called Lighthouse, which is a set of tools developed by Google to test website performance. Specifically, it's using the Chrome DevTools Protocol (CDP) to collect execution time data. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. It's purely a comparison of two simple assignment statements. **Other Alternatives** Some alternative approaches could be: * Using `let` or `const` instead of `var` for the variable name * Using an arrow function (`() => { ... }`) instead of a traditional function declaration * Using a closure to encapsulate the object creation and property access * Using a different data structure, such as an array or object literal, instead of creating a new object on each iteration However, these alternatives would likely not change the fundamental comparison between using `this` and a variable named "that".
Related benchmarks:
Direct call vs bind vs call vs apply vs self
Compare Performance of some JS Design patterns
Direct call vs bind vs call vs apply vs self but makes sense
ES6 Class vs Prototype vs Object Literal v2
ES6 Class vs Prototype vs Object Literal v3
Comments
Confirm delete:
Do you really want to delete benchmark?